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