1 ;;; loadhist.el --- lisp functions for working with feature groups
3 ;; Copyright (C) 1995, 1998, 2000, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
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 2, or (at your option)
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.
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.
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)."
43 (if (member (cons 'provide feature
) (cdr x
))
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.
66 (locate-file file load-path
(get-load-suffixes))))
67 (and absname
(not (equal absname file
))
68 (setq symbols
(cdr (assoc absname load-history
))))))
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
))
78 (if (and (consp x
) (eq (car x
) 'provide
))
79 (setq provides
(cons (cdr x
) 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
))
90 (if (and (consp x
) (eq (car x
) 'require
))
91 (setq requires
(cons (cdr x
) requires
))))
95 (defsubst file-set-intersect
(p q
)
96 "Return the set intersection of two lists."
99 (if (memq x q
) (setq ret
(cons x 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
))
109 (dolist (x load-history dependents
)
110 (if (file-set-intersect provides
(file-requires (car x
)))
111 (setq dependents
(cons (car x
) dependents
))))
114 (defun read-feature (prompt)
115 "Read a feature name \(string\) from the minibuffer.
116 Prompt with PROMPT and completing from `features', and
117 return the feature \(symbol\)."
118 (intern (completing-read prompt
119 (mapcar (lambda (feature)
120 (list (symbol-name feature
)))
124 (defvaralias 'loadhist-hook-functions
'unload-feature-special-hooks
)
125 (defvar unload-feature-special-hooks
126 '(after-change-functions
127 after-insert-file-functions auto-fill-function
128 before-change-functions blink-paren-function
129 buffer-access-fontify-functions command-line-functions
130 comment-indent-function kill-buffer-query-functions
131 kill-emacs-query-functions lisp-indent-function
132 mouse-position-function
133 redisplay-end-trigger-functions temp-buffer-show-function
134 window-scroll-functions window-size-change-functions
135 write-region-annotate-functions
)
136 "A list of special hooks from Info node `(elisp)Standard Hooks'.
138 These are symbols with hook-type values whose names don't end in
139 `-hook' or `-hooks', from which `unload-feature' tries to remove
142 (defvar unload-hook-features-list nil
143 "List of features of the package being unloaded.
145 This is meant to be used by FEATURE-unload-hook hooks, see the
146 documentation of `unload-feature' for details.")
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 modifications made by the package to
155 hooks. Packages may define a hook FEATURE-unload-hook that is called
156 instead of the normal heuristics for doing this. Such a hook should
157 undo all the relevant global state changes that may have been made by
158 loading the package or executing functions in it. It has access to
159 the package's feature list (before anything is unbound) in the
160 variable `unload-hook-features-list' and could remove features from it
161 in the event that the package has done something normally-ill-advised,
162 such as redefining an Emacs function."
163 (interactive (list (read-feature "Feature: ") current-prefix-arg
))
164 (unless (featurep feature
)
165 (error "%s is not a currently loaded feature" (symbol-name feature
)))
167 (let* ((file (feature-file feature
))
168 (dependents (delete file
(copy-sequence (file-dependents file
)))))
170 (error "Loaded libraries %s depend on %s"
171 (prin1-to-string dependents
) file
))))
172 (let* ((unload-hook-features-list (feature-symbols feature
))
173 (file (pop unload-hook-features-list
))
174 (unload-hook (intern-soft (concat (symbol-name feature
)
176 ;; Try to avoid losing badly when hooks installed in critical
177 ;; places go away. (Some packages install things on
178 ;; `kill-buffer-hook', `activate-menubar-hook' and the like.)
179 ;; First off, provide a clean way for package FOO to arrange
180 ;; this by adding hooks on the variable `FOO-unload-hook'.
182 (run-hooks unload-hook
)
183 ;; Otherwise, do our best. Look through the obarray for symbols
184 ;; which seem to be hook variables or special hook functions and
185 ;; remove anything from them which matches the feature-symbols
186 ;; about to get zapped. Obviously this won't get anonymous
187 ;; functions which the package might just have installed, and
188 ;; there might be other important state, but this tactic
192 (when (and (boundp x
)
193 (or (and (consp (symbol-value x
)) ; Random hooks.
194 (string-match "-hooks?\\'" (symbol-name x
)))
195 (memq x unload-feature-special-hooks
))) ; Known abnormal hooks etc.
196 (dolist (y unload-hook-features-list
)
197 (when (and (eq (car-safe y
) 'defun
)
198 (not (get (cdr y
) 'autoload
)))
199 (remove-hook x
(cdr y
)))))))
200 ;; Remove any feature-symbols from auto-mode-alist as well.
201 (dolist (y unload-hook-features-list
)
202 (when (and (eq (car-safe y
) 'defun
)
203 (not (get (cdr y
) 'autoload
)))
204 (setq auto-mode-alist
205 (rassq-delete-all (cdr y
) auto-mode-alist
)))))
206 (when (fboundp 'elp-restore-function
) ; remove ELP stuff first
207 (dolist (elt unload-hook-features-list
)
209 (elp-restore-function elt
))))
210 (dolist (x unload-hook-features-list
)
213 ;; Remove any feature names that this file provided.
215 (setq features
(delq (cdr x
) features
)))
219 (when (fboundp 'ad-unadvise
)
221 (let ((aload (get fun
'autoload
)))
223 (fset fun
(cons 'autoload aload
))
224 (fmakunbound fun
))))))
226 (t (message "Unexpected element %s in load-history" x
)))
227 ;; Kill local values as much as possible.
228 (dolist (buf (buffer-list))
229 (with-current-buffer buf
230 (if (and (boundp x
) (timerp (symbol-value x
)))
231 (cancel-timer (symbol-value x
)))
232 (kill-local-variable x
)))
233 (if (and (boundp x
) (timerp (symbol-value x
)))
234 (cancel-timer (symbol-value x
)))
235 ;; Get rid of the default binding if we can.
236 (unless (local-variable-if-set-p x
)
238 ;; Delete the load-history element for this file.
239 (setq load-history
(delq (assoc file load-history
) load-history
))))
243 ;; arch-tag: 70bb846a-c413-4f01-bf88-78dba4ac0798
244 ;;; loadhist.el ends here