]> code.delx.au - gnu-emacs/blob - lisp/loadhist.el
(handling_window_update, terminate_flag): Remove variables.
[gnu-emacs] / lisp / loadhist.el
1 ;;; loadhist.el --- lisp functions for working with feature groups
2
3 ;; Copyright (C) 1995, 1998, 2000, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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.
25
26 ;;; Commentary:
27
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.
31
32 ;;; Code:
33
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))
44
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))))
53
54 (defun file-loadhist-lookup (file)
55 "Return the `load-history' element for FILE."
56 ;; First look for FILE as given.
57 (let ((symbols (assoc file load-history)))
58 ;; Try converting a library name to an absolute file name.
59 (and (null symbols)
60 (let ((absname (find-library-name file)))
61 (if (not (equal absname file))
62 (setq symbols (cdr (assoc absname load-history))))))
63 ;; Try converting an absolute file name to a library name.
64 (and (null symbols) (string-match "[.]el\\'" file)
65 (let ((libname (file-name-nondirectory file)))
66 (string-match "[.]el\\'" libname)
67 (setq libname (substring libname 0 (match-beginning 0)))
68 (setq symbols (cdr (assoc libname load-history)))))
69 symbols))
70
71 (defun file-provides (file)
72 "Return the list of features provided by FILE."
73 (let ((symbols (file-loadhist-lookup file))
74 provides)
75 (mapc (lambda (x)
76 (if (and (consp x) (eq (car x) 'provide))
77 (setq provides (cons (cdr x) provides))))
78 symbols)
79 provides))
80
81 (defun file-requires (file)
82 "Return the list of features required by FILE."
83 (let ((symbols (file-loadhist-lookup file))
84 requires)
85 (mapc (lambda (x)
86 (if (and (consp x) (eq (car x) 'require))
87 (setq requires (cons (cdr x) requires))))
88 symbols)
89 requires))
90
91 (defsubst file-set-intersect (p q)
92 "Return the set intersection of two lists."
93 (let ((ret nil))
94 (dolist (x p ret)
95 (if (memq x q) (setq ret (cons x ret))))
96 ret))
97
98 (defun file-dependents (file)
99 "Return the list of loaded libraries that depend on FILE.
100 This can include FILE itself."
101 (let ((provides (file-provides file))
102 (dependents nil))
103 (dolist (x load-history dependents)
104 (if (file-set-intersect provides (file-requires (car x)))
105 (setq dependents (cons (car x) dependents))))
106 dependents))
107
108 (defun read-feature (prompt)
109 "Read a feature name \(string\) from the minibuffer.
110 Prompt with PROMPT and completing from `features', and
111 return the feature \(symbol\)."
112 (intern (completing-read prompt
113 (mapcar (lambda (feature)
114 (list (symbol-name feature)))
115 features)
116 nil t)))
117
118 (defvaralias 'loadhist-hook-functions 'unload-feature-special-hooks)
119 (defvar unload-feature-special-hooks
120 '(after-change-functions
121 after-insert-file-functions auto-fill-function
122 before-change-functions blink-paren-function
123 buffer-access-fontify-functions command-line-functions
124 comment-indent-function kill-buffer-query-functions
125 kill-emacs-query-functions lisp-indent-function
126 mouse-position-function
127 redisplay-end-trigger-functions temp-buffer-show-function
128 window-scroll-functions window-size-change-functions
129 write-region-annotate-functions)
130 "A list of special hooks from Info node `(elisp)Standard Hooks'.
131
132 These are symbols with hook-type values whose names don't end in
133 `-hook' or `-hooks', from which `unload-feature' tries to remove
134 pertinent symbols.")
135
136 (defvar unload-hook-features-list nil
137 "List of features of the package being unloaded.
138
139 This is meant to be used by FEATURE-unload-hook hooks, see the
140 documentation of `unload-feature' for details.")
141
142 ;;;###autoload
143 (defun unload-feature (feature &optional force)
144 "Unload the library that provided FEATURE, restoring all its autoloads.
145 If the feature is required by any other loaded code, and prefix arg FORCE
146 is nil, raise an error.
147
148 This function tries to undo modifications made by the package to
149 hooks. Packages may define a hook FEATURE-unload-hook that is called
150 instead of the normal heuristics for doing this. Such a hook should
151 undo all the relevant global state changes that may have been made by
152 loading the package or executing functions in it. It has access to
153 the package's feature list (before anything is unbound) in the
154 variable `unload-hook-features-list' and could remove features from it
155 in the event that the package has done something normally-ill-advised,
156 such as redefining an Emacs function."
157 (interactive (list (read-feature "Feature: ") current-prefix-arg))
158 (unless (featurep feature)
159 (error "%s is not a currently loaded feature" (symbol-name feature)))
160 (unless force
161 (let* ((file (feature-file feature))
162 (dependents (delete file (copy-sequence (file-dependents file)))))
163 (when dependents
164 (error "Loaded libraries %s depend on %s"
165 (prin1-to-string dependents) file))))
166 (let* ((unload-hook-features-list (feature-symbols feature))
167 (file (pop unload-hook-features-list))
168 (unload-hook (intern-soft (concat (symbol-name feature)
169 "-unload-hook"))))
170 ;; Try to avoid losing badly when hooks installed in critical
171 ;; places go away. (Some packages install things on
172 ;; `kill-buffer-hook', `activate-menubar-hook' and the like.)
173 ;; First off, provide a clean way for package FOO to arrange
174 ;; this by adding hooks on the variable `FOO-unload-hook'.
175 (if unload-hook
176 (run-hooks unload-hook)
177 ;; Otherwise, do our best. Look through the obarray for symbols
178 ;; which seem to be hook variables or special hook functions and
179 ;; remove anything from them which matches the feature-symbols
180 ;; about to get zapped. Obviously this won't get anonymous
181 ;; functions which the package might just have installed, and
182 ;; there might be other important state, but this tactic
183 ;; normally works.
184 (mapatoms
185 (lambda (x)
186 (when (and (boundp x)
187 (or (and (consp (symbol-value x)) ; Random hooks.
188 (string-match "-hooks?\\'" (symbol-name x)))
189 (memq x unload-feature-special-hooks))) ; Known abnormal hooks etc.
190 (dolist (y unload-hook-features-list)
191 (when (and (eq (car-safe y) 'defun)
192 (not (get (cdr y) 'autoload)))
193 (remove-hook x (cdr y)))))))
194 ;; Remove any feature-symbols from auto-mode-alist as well.
195 (dolist (y unload-hook-features-list)
196 (when (and (eq (car-safe y) 'defun)
197 (not (get (cdr y) 'autoload)))
198 (setq auto-mode-alist
199 (rassq-delete-all (cdr y) auto-mode-alist)))))
200 (when (fboundp 'elp-restore-function) ; remove ELP stuff first
201 (dolist (elt unload-hook-features-list)
202 (when (symbolp elt)
203 (elp-restore-function elt))))
204 (dolist (x unload-hook-features-list)
205 (if (consp x)
206 (cond
207 ;; Remove any feature names that this file provided.
208 ((eq (car x) 'provide)
209 (setq features (delq (cdr x) features)))
210 ((eq (car x) 'defun)
211 (let ((fun (cdr x)))
212 (when (fboundp fun)
213 (when (fboundp 'ad-unadvise)
214 (ad-unadvise fun))
215 (fmakunbound fun)
216 (let ((aload (get fun 'autoload)))
217 (when aload
218 (fset fun (cons 'autoload aload))))))))
219 ;; Kill local values as much as possible.
220 (dolist (buf (buffer-list))
221 (with-current-buffer buf
222 (kill-local-variable x)))
223 ;; Get rid of the default binding if we can.
224 (unless (local-variable-if-set-p x)
225 (makunbound x))))
226 ;; Delete the load-history element for this file.
227 (setq load-history (delq (assoc file load-history) load-history))))
228
229 (provide 'loadhist)
230
231 ;;; arch-tag: 70bb846a-c413-4f01-bf88-78dba4ac0798
232 ;;; loadhist.el ends here