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