(setup_for_ellipsis): Reset saved_face_id to use default
[bpt/emacs.git] / lisp / loadhist.el
CommitLineData
2b86d62c 1;;; loadhist.el --- lisp functions for working with feature groups
b578f267 2
53c1fa0f 3;; Copyright (C) 1995, 1998, 2000 Free Software Foundation, Inc.
2b86d62c
RS
4
5;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
53c1fa0f 6;; Maintainer: FSF
2b86d62c
RS
7;; Keywords: internal
8
131a0c01
KH
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
b578f267
EN
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.
131a0c01 25
2b86d62c
RS
26;;; Commentary:
27
28;; These functions exploit the load-history system variable.
fde4581d
RS
29;; Entry points include `unload-feature', `symbol-file', and
30;; `feature-file', documented in the Emacs Lisp manual.
2b86d62c
RS
31
32;;; Code:
33
2b86d62c 34(defun feature-symbols (feature)
7dc2cc98
RS
35 "Return the file and list of definitions associated with FEATURE.
36The value is actually the element of `load-history'
37for the file that did (provide FEATURE)."
2b86d62c 38 (catch 'foundit
53c1fa0f
DL
39 (mapc (lambda (x)
40 (if (member (cons 'provide feature) (cdr x))
41 (throw 'foundit x)))
42 load-history)
2b86d62c
RS
43 nil))
44
45(defun feature-file (feature)
46 "Return the file name from which a given FEATURE was loaded.
47Actually, return the load argument, if any; this is sometimes the name of a
53c1fa0f
DL
48Lisp file without an extension. If the feature came from an `eval-buffer' on
49a buffer with no associated file, or an `eval-region', return nil."
2b86d62c 50 (if (not (featurep feature))
53c1fa0f 51 (error "%S is not a currently loaded feature" feature)
2b86d62c
RS
52 (car (feature-symbols feature))))
53
27af61fe
RS
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
2b86d62c
RS
71(defun file-provides (file)
72 "Return the list of features provided by FILE."
27af61fe 73 (let ((symbols (file-loadhist-lookup file))
53c1fa0f
DL
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))
2b86d62c
RS
80
81(defun file-requires (file)
82 "Return the list of features required by FILE."
27af61fe 83 (let ((symbols (file-loadhist-lookup file))
53c1fa0f
DL
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."
2b86d62c 93 (let ((ret nil))
53c1fa0f
DL
94 (dolist (x p ret)
95 (if (memq x q) (setq ret (cons x ret))))
96 ret))
2b86d62c
RS
97
98(defun file-dependents (file)
e01ac82c
RS
99 "Return the list of loaded libraries that depend on FILE.
100This can include FILE itself."
53c1fa0f
DL
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))
2b86d62c 107
d4708676 108(defun read-feature (prompt)
fde4581d
RS
109 "Read a feature name \(string\) from the minibuffer.
110Prompt with PROMPT and completing from `features', and
d4708676
RS
111return the feature \(symbol\)."
112 (intern (completing-read prompt
53c1fa0f
DL
113 (mapcar (lambda (feature)
114 (list (symbol-name feature)))
d4708676
RS
115 features)
116 nil t)))
117
28d02da1
RS
118(defvaralias 'loadhist-hook-functions 'unload-feature-special-hooks)
119(defvar unload-feature-special-hooks
28d8dff1 120 '(after-change-functions
7dc2cc98
RS
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)
3eef341a 130 "A list of special hooks from Info node `(elisp)Standard Hooks'.
fde4581d
RS
131
132These are symbols with hook-type values whose names don't end in
133`-hook' or `-hooks', from which `unload-feature' tries to remove
134pertinent symbols.")
135
dffc4dfc
EZ
136(defvar unload-hook-features-list nil
137 "List of features of the package being unloaded.
138
139This is meant to be used by FEATURE-unload-hook hooks, see the
140documentation of `unload-feature' for details.")
141
2b86d62c
RS
142;;;###autoload
143(defun unload-feature (feature &optional force)
144 "Unload the library that provided FEATURE, restoring all its autoloads.
4370a375 145If the feature is required by any other loaded code, and prefix arg FORCE
8cb16ad1
EZ
146is nil, raise an error.
147
148This function tries to undo modifications made by the package to
149hooks. Packages may define a hook FEATURE-unload-hook that is called
150instead of the normal heuristics for doing this. Such a hook should
151undo all the relevant global state changes that may have been made by
152loading the package or executing functions in it. It has access to
153the package's feature list (before anything is unbound) in the
154variable `unload-hook-features-list' and could remove features from it
155in the event that the package has done something normally-ill-advised,
156such as redefining an Emacs function."
4370a375 157 (interactive (list (read-feature "Feature: ") current-prefix-arg))
2b86d62c 158 (if (not (featurep feature))
a5c31fa1 159 (error "%s is not a currently loaded feature" (symbol-name feature)))
2b86d62c 160 (if (not force)
e01ac82c
RS
161 (let* ((file (feature-file feature))
162 (dependents (delete file (copy-sequence (file-dependents file)))))
2b86d62c 163 (if dependents
a5c31fa1 164 (error "Loaded libraries %s depend on %s"
fde4581d 165 (prin1-to-string dependents) file))))
8cb16ad1
EZ
166 (let* ((unload-hook-features-list (feature-symbols feature))
167 (file (car unload-hook-features-list))
fde4581d
RS
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.)
557be036
RS
173 ;; First off, provide a clean way for package FOO to arrange
174 ;; this by adding hooks on the variable `FOO-unload-hook'.
fde4581d
RS
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 (if (or (and (boundp x) ; Random hooks.
187 (consp (symbol-value x))
188 (string-match "-hooks?\\'" (symbol-name x)))
057ab294 189 (and (boundp x) ; Known abnormal hooks etc.
28d02da1 190 (memq x unload-feature-special-hooks)))
8cb16ad1 191 (dolist (y (cdr unload-hook-features-list))
53c1fa0f 192 (remove-hook x y))))))
3eef341a 193 (if (fboundp 'elp-restore-function) ; remove ELP stuff first
8cb16ad1 194 (dolist (elt (cdr unload-hook-features-list))
3eef341a
DL
195 (if (symbolp elt)
196 (elp-restore-function elt))))
23ce2deb
RS
197 (dolist (x (cdr unload-hook-features-list))
198 (when (consp x)
199 ;; Remove any feature names that this file provided.
200 (if (eq (car x) 'provide)
201 (setq features (delq (cdr x) features)))
202 (when (eq (car x) 'defvar)
203 ;; Kill local values as much as possible.
204 (dolist (buf (buffer-list))
205 (with-current-buffer buf
206 (kill-local-variable (cdr x))))
207 ;; Get rid of the default binding if we can.
208 (unless (local-variable-if-set-p (cdr x))
209 (makunbound (cdr x))))
210 (when (eq (car x) 'defun)
211 (let ((fun (cdr x)))
212 (when (fboundp fun)
213 (if (fboundp 'ad-unadvise)
214 (ad-unadvise fun))
215 (fmakunbound fun)
216 (let ((aload (get fun 'autoload)))
217 (if aload (fset fun (cons 'autoload aload)))))))))
a5c31fa1
RS
218 ;; Delete the load-history element for this file.
219 (let ((elt (assoc file load-history)))
220 (setq load-history (delq elt load-history)))))
2b86d62c
RS
221
222(provide 'loadhist)
223
ab5796a9 224;;; arch-tag: 70bb846a-c413-4f01-bf88-78dba4ac0798
2b86d62c 225;;; loadhist.el ends here