(diff-default-read-only): Change default.
[emacs.git] / lisp / loadhist.el
blob8cbe1d80cd39864fb87a75f91473327530c03580
1 ;;; loadhist.el --- lisp functions for working with feature groups
3 ;; Copyright (C) 1995, 1998, 2000 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Maintainer: FSF
7 ;; Keywords: internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; These functions exploit the load-history system variable.
29 ;; Entry points include `unload-feature', `symbol-file', and
30 ;; `feature-file', documented in the Emacs Lisp manual.
32 ;;; Code:
34 (defun feature-symbols (feature)
35 "Return the file and list of definitions associated with FEATURE.
36 The value is actually the element of `load-history'
37 for the file that did (provide FEATURE)."
38 (catch 'foundit
39 (mapc (lambda (x)
40 (if (member (cons 'provide feature) (cdr x))
41 (throw 'foundit x)))
42 load-history)
43 nil))
45 (defun feature-file (feature)
46 "Return the file name from which a given FEATURE was loaded.
47 Actually, return the load argument, if any; this is sometimes the name of a
48 Lisp file without an extension. If the feature came from an `eval-buffer' on
49 a buffer with no associated file, or an `eval-region', return nil."
50 (if (not (featurep feature))
51 (error "%S is not a currently loaded feature" feature)
52 (car (feature-symbols feature))))
54 (defun file-provides (file)
55 "Return the list of features provided by FILE."
56 (let ((symbols (cdr (assoc file load-history)))
57 provides)
58 (mapc (lambda (x)
59 (if (and (consp x) (eq (car x) 'provide))
60 (setq provides (cons (cdr x) provides))))
61 symbols)
62 provides))
64 (defun file-requires (file)
65 "Return the list of features required by FILE."
66 (let ((symbols (cdr (assoc file load-history)))
67 requires)
68 (mapc (lambda (x)
69 (if (and (consp x) (eq (car x) 'require))
70 (setq requires (cons (cdr x) requires))))
71 symbols)
72 requires))
74 (defsubst file-set-intersect (p q)
75 "Return the set intersection of two lists."
76 (let ((ret nil))
77 (dolist (x p ret)
78 (if (memq x q) (setq ret (cons x ret))))
79 ret))
81 (defun file-dependents (file)
82 "Return the list of loaded libraries that depend on FILE.
83 This can include FILE itself."
84 (let ((provides (file-provides file))
85 (dependents nil))
86 (dolist (x load-history dependents)
87 (if (file-set-intersect provides (file-requires (car x)))
88 (setq dependents (cons (car x) dependents))))
89 dependents))
91 (defun read-feature (prompt)
92 "Read a feature name \(string\) from the minibuffer.
93 Prompt with PROMPT and completing from `features', and
94 return the feature \(symbol\)."
95 (intern (completing-read prompt
96 (mapcar (lambda (feature)
97 (list (symbol-name feature)))
98 features)
99 nil t)))
101 (defvaralias 'loadhist-hook-functions 'unload-feature-special-hooks)
102 (defvar unload-feature-special-hooks
103 '(after-change-functions
104 after-insert-file-functions auto-fill-function
105 before-change-functions blink-paren-function
106 buffer-access-fontify-functions command-line-functions
107 comment-indent-function kill-buffer-query-functions
108 kill-emacs-query-functions lisp-indent-function
109 mouse-position-function
110 redisplay-end-trigger-functions temp-buffer-show-function
111 window-scroll-functions window-size-change-functions
112 write-region-annotate-functions)
113 "A list of special hooks from Info node `(elisp)Standard Hooks'.
115 These are symbols with hook-type values whose names don't end in
116 `-hook' or `-hooks', from which `unload-feature' tries to remove
117 pertinent symbols.")
119 (defvar unload-hook-features-list nil
120 "List of features of the package being unloaded.
122 This is meant to be used by FEATURE-unload-hook hooks, see the
123 documentation of `unload-feature' for details.")
125 ;;;###autoload
126 (defun unload-feature (feature &optional force)
127 "Unload the library that provided FEATURE, restoring all its autoloads.
128 If the feature is required by any other loaded code, and prefix arg FORCE
129 is nil, raise an error.
131 This function tries to undo modifications made by the package to
132 hooks. Packages may define a hook FEATURE-unload-hook that is called
133 instead of the normal heuristics for doing this. Such a hook should
134 undo all the relevant global state changes that may have been made by
135 loading the package or executing functions in it. It has access to
136 the package's feature list (before anything is unbound) in the
137 variable `unload-hook-features-list' and could remove features from it
138 in the event that the package has done something normally-ill-advised,
139 such as redefining an Emacs function."
140 (interactive (list (read-feature "Feature: ") current-prefix-arg))
141 (if (not (featurep feature))
142 (error "%s is not a currently loaded feature" (symbol-name feature)))
143 (if (not force)
144 (let* ((file (feature-file feature))
145 (dependents (delete file (copy-sequence (file-dependents file)))))
146 (if dependents
147 (error "Loaded libraries %s depend on %s"
148 (prin1-to-string dependents) file))))
149 (let* ((unload-hook-features-list (feature-symbols feature))
150 (file (car unload-hook-features-list))
151 (unload-hook (intern-soft (concat (symbol-name feature)
152 "-unload-hook"))))
153 ;; Try to avoid losing badly when hooks installed in critical
154 ;; places go away. (Some packages install things on
155 ;; `kill-buffer-hook', `activate-menubar-hook' and the like.)
156 ;; First off, provide a clean way for package FOO to arrange
157 ;; this by adding hooks on the variable `FOO-unload-hook'.
158 (if unload-hook
159 (run-hooks unload-hook)
160 ;; Otherwise, do our best. Look through the obarray for symbols
161 ;; which seem to be hook variables or special hook functions and
162 ;; remove anything from them which matches the feature-symbols
163 ;; about to get zapped. Obviously this won't get anonymous
164 ;; functions which the package might just have installed, and
165 ;; there might be other important state, but this tactic
166 ;; normally works.
167 (mapatoms
168 (lambda (x)
169 (if (or (and (boundp x) ; Random hooks.
170 (consp (symbol-value x))
171 (string-match "-hooks?\\'" (symbol-name x)))
172 (and (boundp x) ; Known abnormal hooks etc.
173 (memq x unload-feature-special-hooks)))
174 (dolist (y (cdr unload-hook-features-list))
175 (remove-hook x y))))))
176 (if (fboundp 'elp-restore-function) ; remove ELP stuff first
177 (dolist (elt (cdr unload-hook-features-list))
178 (if (symbolp elt)
179 (elp-restore-function elt))))
180 (mapc
181 (lambda (x)
182 (cond ((stringp x) nil)
183 ((consp x)
184 ;; Remove any feature names that this file provided.
185 (if (eq (car x) 'provide)
186 (setq features (delq (cdr x) features)))
187 (when (eq (car x) 'defvar)
188 ;; Kill local values as much as possible.
189 (dolist (buf (buffer-list))
190 (with-current-buffer buf
191 (kill-local-variable (cdr x))))
192 ;; Get rid of the default binding if we can.
193 (unless (local-variable-if-set-p (cdr x))
194 (makunbound (cdr x)))))
196 (when (fboundp x)
197 (if (fboundp 'ad-unadvise)
198 (ad-unadvise x))
199 (fmakunbound x)
200 (let ((aload (get x 'autoload)))
201 (if aload (fset x (cons 'autoload aload))))))))
202 (cdr unload-hook-features-list))
203 ;; Delete the load-history element for this file.
204 (let ((elt (assoc file load-history)))
205 (setq load-history (delq elt load-history)))))
207 (provide 'loadhist)
209 ;;; arch-tag: 70bb846a-c413-4f01-bf88-78dba4ac0798
210 ;;; loadhist.el ends here