src/gnutls.c: Remove bogus references to :verify-error.
[emacs.git] / lisp / loadhist.el
blob943eac42b0205dfa7db8953c311c76623257063f
1 ;;; loadhist.el --- lisp functions for working with feature groups
3 ;; Copyright (C) 1995, 1998, 2000-2011 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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; These functions exploit the load-history system variable.
27 ;; Entry points include `unload-feature', `symbol-file', and
28 ;; `feature-file', documented in the Emacs Lisp manual.
30 ;;; Code:
32 (eval-when-compile (require 'cl))
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 (let ((element (cons 'provide feature)))
40 (dolist (x load-history nil)
41 (when (member element (cdr x))
42 (throw 'foundit x))))))
44 (defun feature-file (feature)
45 "Return the file name from which a given FEATURE was loaded.
46 Actually, return the load argument, if any; this is sometimes the name of a
47 Lisp file without an extension. If the feature came from an `eval-buffer' on
48 a buffer with no associated file, or an `eval-region', return nil."
49 (if (not (featurep feature))
50 (error "%S is not a currently loaded feature" feature)
51 (car (feature-symbols feature))))
53 (defun file-loadhist-lookup (file)
54 "Return the `load-history' element for FILE.
55 FILE can be a file name, or a library name.
56 A library name is equivalent to the file name that `load-library' would load."
57 ;; First look for FILE as given.
58 (let ((symbols (assoc file load-history)))
59 ;; Try converting a library name to an absolute file name.
60 (and (null symbols)
61 (let ((absname
62 (locate-file file load-path (get-load-suffixes))))
63 (and absname (not (equal absname file))
64 (setq symbols (cdr (assoc absname load-history))))))
65 symbols))
67 (defun file-provides (file)
68 "Return the list of features provided by FILE as it was loaded.
69 FILE can be a file name, or a library name.
70 A library name is equivalent to the file name that `load-library' would load."
71 (let (provides)
72 (dolist (x (file-loadhist-lookup file) provides)
73 (when (eq (car-safe x) 'provide)
74 (push (cdr x) provides)))))
76 (defun file-requires (file)
77 "Return the list of features required by FILE as it was loaded.
78 FILE can be a file name, or a library name.
79 A library name is equivalent to the file name that `load-library' would load."
80 (let (requires)
81 (dolist (x (file-loadhist-lookup file) requires)
82 (when (eq (car-safe x) 'require)
83 (push (cdr x) requires)))))
85 (defsubst file-set-intersect (p q)
86 "Return the set intersection of two lists."
87 (let (ret)
88 (dolist (x p ret)
89 (when (memq x q) (push x ret)))))
91 (defun file-dependents (file)
92 "Return the list of loaded libraries that depend on FILE.
93 This can include FILE itself.
94 FILE can be a file name, or a library name.
95 A library name is equivalent to the file name that `load-library' would load."
96 (let ((provides (file-provides file))
97 (dependents nil))
98 (dolist (x load-history dependents)
99 (when (file-set-intersect provides (file-requires (car x)))
100 (push (car x) dependents)))))
102 (defun read-feature (prompt &optional loaded-p)
103 "Read feature name from the minibuffer, prompting with string PROMPT.
104 If optional second arg LOADED-P is non-nil, the feature must be loaded
105 from a file."
106 (intern
107 (completing-read prompt
108 (cons nil features)
109 (and loaded-p
110 #'(lambda (f)
111 (and f ; ignore nil
112 (feature-file f))))
113 loaded-p)))
115 (defvaralias 'loadhist-hook-functions 'unload-feature-special-hooks)
116 (defvar unload-feature-special-hooks
117 '(after-change-functions after-insert-file-functions
118 after-make-frame-functions auto-fill-function before-change-functions
119 blink-paren-function buffer-access-fontify-functions
120 choose-completion-string-functions comint-output-filter-functions
121 command-line-functions comment-indent-function compilation-finish-functions
122 delete-frame-functions disabled-command-function
123 find-file-not-found-functions font-lock-beginning-of-syntax-function
124 font-lock-fontify-buffer-function font-lock-fontify-region-function
125 font-lock-mark-block-function font-lock-syntactic-face-function
126 font-lock-unfontify-buffer-function font-lock-unfontify-region-function
127 kill-buffer-query-functions kill-emacs-query-functions lisp-indent-function
128 mouse-position-function redisplaylay-end-trigger-functions
129 suspend-tty-functions temp-buffer-show-function window-scroll-functions
130 window-size-change-functions write-contents-functions write-file-functions
131 write-region-annotate-functions)
132 "A list of special hooks from Info node `(elisp)Standard Hooks'.
134 These are symbols with hooklike values whose names don't end in
135 `-hook' or `-hooks', from which `unload-feature' should try to remove
136 pertinent symbols.")
138 (defvar unload-function-defs-list nil
139 "List of defintions in the Lisp library being unloaded.
141 This is meant to be used by `FEATURE-unload-function'; see the
142 documentation of `unload-feature' for details.")
143 (define-obsolete-variable-alias 'unload-hook-features-list
144 'unload-function-defs-list "22.2")
146 (defun unload--set-major-mode ()
147 (save-current-buffer
148 (dolist (buffer (buffer-list))
149 (set-buffer buffer)
150 (let ((proposed major-mode))
151 ;; Look for an antecessor mode not defined in the feature we're processing
152 (while (and proposed (rassq proposed unload-function-defs-list))
153 (setq proposed (get proposed 'derived-mode-parent)))
154 (unless (eq proposed major-mode)
155 ;; Two cases: either proposed is nil, and we want to switch to fundamental
156 ;; mode, or proposed is not nil and not major-mode, and so we use it.
157 (funcall (or proposed 'fundamental-mode)))))))
159 ;;;###autoload
160 (defun unload-feature (feature &optional force)
161 "Unload the library that provided FEATURE.
162 If the feature is required by any other loaded code, and prefix arg FORCE
163 is nil, raise an error.
165 Standard unloading activities include restoring old autoloads for
166 functions defined by the library, undoing any additions that the
167 library has made to hook variables or to `auto-mode-alist', undoing
168 ELP profiling of functions in that library, unproviding any features
169 provided by the library, and canceling timers held in variables
170 defined by the library.
172 If a function `FEATURE-unload-function' is defined, this function
173 calls it with no arguments, before doing anything else. That function
174 can do whatever is appropriate to undo the loading of the library. If
175 `FEATURE-unload-function' returns non-nil, that suppresses the
176 standard unloading of the library. Otherwise the standard unloading
177 proceeds.
179 `FEATURE-unload-function' has access to the package's list of
180 definitions in the variable `unload-function-defs-list' and could
181 remove symbols from it in the event that the package has done
182 something strange, such as redefining an Emacs function."
183 (interactive
184 (list
185 (read-feature "Unload feature: " t)
186 current-prefix-arg))
187 (unless (featurep feature)
188 (error "%s is not a currently loaded feature" (symbol-name feature)))
189 (unless force
190 (let* ((file (feature-file feature))
191 (dependents (delete file (copy-sequence (file-dependents file)))))
192 (when dependents
193 (error "Loaded libraries %s depend on %s"
194 (prin1-to-string dependents) file))))
195 (let* ((unload-function-defs-list (feature-symbols feature))
196 (file (pop unload-function-defs-list))
197 ;; If non-nil, this is a symbol for which we should
198 ;; restore a previous autoload if possible.
199 restore-autoload
200 (name (symbol-name feature))
201 (unload-hook (intern-soft (concat name "-unload-hook")))
202 (unload-func (intern-soft (concat name "-unload-function"))))
203 ;; If FEATURE-unload-function is defined and returns non-nil,
204 ;; don't try to do anything more; otherwise proceed normally.
205 (unless (and (fboundp unload-func)
206 (funcall unload-func))
207 ;; Try to avoid losing badly when hooks installed in critical
208 ;; places go away. (Some packages install things on
209 ;; `kill-buffer-hook', `activate-menubar-hook' and the like.)
210 (if unload-hook
211 ;; First off, provide a clean way for package FOO to arrange
212 ;; this by adding hooks on the variable `FOO-unload-hook'.
213 ;; This is obsolete; FEATURE-unload-function should be used now.
214 (run-hooks unload-hook)
215 ;; Otherwise, do our best. Look through the obarray for symbols
216 ;; which seem to be hook variables or special hook functions and
217 ;; remove anything from them which matches the feature-symbols
218 ;; about to get zapped. Obviously this won't get anonymous
219 ;; functions which the package might just have installed, and
220 ;; there might be other important state, but this tactic
221 ;; normally works.
222 (mapatoms
223 (lambda (x)
224 (when (and (boundp x)
225 (or (and (consp (symbol-value x)) ; Random hooks.
226 (string-match "-hooks?\\'" (symbol-name x)))
227 (memq x unload-feature-special-hooks))) ; Known abnormal hooks etc.
228 (dolist (y unload-function-defs-list)
229 (when (and (eq (car-safe y) 'defun)
230 (not (get (cdr y) 'autoload)))
231 (remove-hook x (cdr y)))))))
232 ;; Remove any feature-symbols from auto-mode-alist as well.
233 (dolist (y unload-function-defs-list)
234 (when (and (eq (car-safe y) 'defun)
235 (not (get (cdr y) 'autoload)))
236 (setq auto-mode-alist
237 (rassq-delete-all (cdr y) auto-mode-alist)))))
239 ;; Change major mode in all buffers using one defined in the feature being unloaded.
240 (unload--set-major-mode)
242 (when (fboundp 'elp-restore-function) ; remove ELP stuff first
243 (dolist (elt unload-function-defs-list)
244 (when (symbolp elt)
245 (elp-restore-function elt))))
247 (dolist (x unload-function-defs-list)
248 (if (consp x)
249 (case (car x)
250 ;; Remove any feature names that this file provided.
251 (provide
252 (setq features (delq (cdr x) features)))
253 ((defun autoload)
254 (let ((fun (cdr x)))
255 (when (fboundp fun)
256 (when (fboundp 'ad-unadvise)
257 (ad-unadvise fun))
258 (let ((aload (get fun 'autoload)))
259 (if (and aload (eq fun restore-autoload))
260 (fset fun (cons 'autoload aload))
261 (fmakunbound fun))))))
262 ;; (t . SYMBOL) comes before (defun . SYMBOL)
263 ;; and says we should restore SYMBOL's autoload
264 ;; when we undefine it.
265 ((t) (setq restore-autoload (cdr x)))
266 ((require defface) nil)
267 (t (message "Unexpected element %s in load-history" x)))
268 ;; Kill local values as much as possible.
269 (dolist (buf (buffer-list))
270 (with-current-buffer buf
271 (if (and (boundp x) (timerp (symbol-value x)))
272 (cancel-timer (symbol-value x)))
273 (kill-local-variable x)))
274 (if (and (boundp x) (timerp (symbol-value x)))
275 (cancel-timer (symbol-value x)))
276 ;; Get rid of the default binding if we can.
277 (unless (local-variable-if-set-p x)
278 (makunbound x))))
279 ;; Delete the load-history element for this file.
280 (setq load-history (delq (assoc file load-history) load-history))))
281 ;; Don't return load-history, it is not useful.
282 nil)
284 (provide 'loadhist)
286 ;;; loadhist.el ends here