(read-feature): Doc fix.
[bpt/emacs.git] / lisp / loadhist.el
1 ;;; loadhist.el --- lisp functions for working with feature groups
2
3 ;; Copyright (C) 1995, 1998 Free Software Foundation, Inc.
4
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Version: 1.0
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 symbol-file (sym)
35 "Return the input source from which SYM was loaded.
36 This is a file name, or nil if the source was a buffer with no associated file."
37 (catch 'foundit
38 (mapcar
39 (function (lambda (x) (if (memq sym (cdr x)) (throw 'foundit (car x)))))
40 load-history)
41 nil))
42
43 (defun feature-symbols (feature)
44 "Return the file and list of symbols associated with a given FEATURE."
45 (catch 'foundit
46 (mapcar
47 (function (lambda (x)
48 (if (member (cons 'provide feature) (cdr x))
49 (throw 'foundit x))))
50 load-history)
51 nil))
52
53 (defun feature-file (feature)
54 "Return the file name from which a given FEATURE was loaded.
55 Actually, return the load argument, if any; this is sometimes the name of a
56 Lisp file without an extension. If the feature came from an eval-buffer on
57 a buffer with no associated file, or an eval-region, return nil."
58 (if (not (featurep feature))
59 (error "%s is not a currently loaded feature" (symbol-name feature))
60 (car (feature-symbols feature))))
61
62 (defun file-provides (file)
63 "Return the list of features provided by FILE."
64 (let ((symbols (cdr (assoc file load-history))) (provides nil))
65 (mapcar
66 (function (lambda (x)
67 (if (and (consp x) (eq (car x) 'provide))
68 (setq provides (cons (cdr x) provides)))))
69 symbols)
70 provides
71 ))
72
73 (defun file-requires (file)
74 "Return the list of features required by FILE."
75 (let ((symbols (cdr (assoc file load-history))) (requires nil))
76 (mapcar
77 (function (lambda (x)
78 (if (and (consp x) (eq (car x) 'require))
79 (setq requires (cons (cdr x) requires)))))
80 symbols)
81 requires
82 ))
83
84 (defun file-set-intersect (p q)
85 ;; Return the set intersection of two lists
86 (let ((ret nil))
87 (mapcar
88 (function (lambda (x) (if (memq x q) (setq ret (cons x ret)))))
89 p)
90 ret
91 ))
92
93 (defun file-dependents (file)
94 "Return the list of loaded libraries that depend on FILE.
95 This can include FILE itself."
96 (let ((provides (file-provides file)) (dependents nil))
97 (mapcar
98 (function (lambda (x)
99 (if (file-set-intersect provides (file-requires (car x)))
100 (setq dependents (cons (car x) dependents)))))
101 load-history)
102 dependents
103 ))
104
105 (defun read-feature (prompt)
106 "Read a feature name \(string\) from the minibuffer.
107 Prompt with PROMPT and completing from `features', and
108 return the feature \(symbol\)."
109 (intern (completing-read prompt
110 (mapcar (function (lambda (feature)
111 (list (symbol-name feature))))
112 features)
113 nil t)))
114
115 (defvar loadhist-hook-functions
116 '(after-change-function after-change-functions
117 after-insert-file-functions auto-fill-function before-change-function
118 before-change-functions blink-paren-function
119 buffer-access-fontify-functions command-line-functions
120 comment-indent-function kill-buffer-query-functions
121 kill-emacs-query-functions lisp-indent-function
122 redisplay-end-trigger-functions temp-buffer-show-function
123 window-scroll-functions window-size-change-functions
124 write-region-annotate-functions)
125 "A list of special hooks from the `Standard Hooks' node of the Lisp manual.
126
127 These are symbols with hook-type values whose names don't end in
128 `-hook' or `-hooks', from which `unload-feature' tries to remove
129 pertinent symbols.")
130
131 ;;;###autoload
132 (defun unload-feature (feature &optional force)
133 "Unload the library that provided FEATURE, restoring all its autoloads.
134 If the feature is required by any other loaded code, and optional FORCE
135 is nil, raise an error."
136 (interactive (list (read-feature "Feature: ")))
137 (if (not (featurep feature))
138 (error "%s is not a currently loaded feature" (symbol-name feature)))
139 (if (not force)
140 (let* ((file (feature-file feature))
141 (dependents (delete file (copy-sequence (file-dependents file)))))
142 (if dependents
143 (error "Loaded libraries %s depend on %s"
144 (prin1-to-string dependents) file))))
145 (let* ((flist (feature-symbols feature))
146 (file (car flist))
147 (unload-hook (intern-soft (concat (symbol-name feature)
148 "-unload-hook"))))
149 ;; Try to avoid losing badly when hooks installed in critical
150 ;; places go away. (Some packages install things on
151 ;; `kill-buffer-hook', `activate-menubar-hook' and the like.)
152 ;; First off, provide a clean way for package `foo' to arrange
153 ;; this by defining `foo-unload-hook'.
154 (if unload-hook
155 (run-hooks unload-hook)
156 ;; Otherwise, do our best. Look through the obarray for symbols
157 ;; which seem to be hook variables or special hook functions and
158 ;; remove anything from them which matches the feature-symbols
159 ;; about to get zapped. Obviously this won't get anonymous
160 ;; functions which the package might just have installed, and
161 ;; there might be other important state, but this tactic
162 ;; normally works.
163 (mapatoms
164 (lambda (x)
165 (if (or (and (boundp x) ; Random hooks.
166 (consp (symbol-value x))
167 (string-match "-hooks?\\'" (symbol-name x)))
168 (and (fboundp x) ; Known abnormal hooks etc.
169 (memq x loadhist-hook-functions)))
170 (mapcar (lambda (y) (remove-hook x y))
171 (cdr flist))))))
172 (mapcar
173 (lambda (x)
174 (cond ((stringp x) nil)
175 ((consp x)
176 ;; Remove any feature names that this file provided.
177 (if (eq (car x) 'provide)
178 (setq features (delq (cdr x) features))))
179 ((boundp x) (makunbound x))
180 ((fboundp x)
181 (fmakunbound x)
182 (let ((aload (get x 'autoload)))
183 (if aload (fset x (cons 'autoload aload)))))))
184 (cdr flist))
185 ;; Delete the load-history element for this file.
186 (let ((elt (assoc file load-history)))
187 (setq load-history (delq elt load-history)))))
188
189 (provide 'loadhist)
190
191 ;;; loadhist.el ends here