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