Mercurial > emacs
annotate lisp/loadhist.el @ 88332:2a74a9280eba
(rmail-edit-map): Initialize at definition. Doc String.
author | Henrik Enberg <henrik.enberg@telia.com> |
---|---|
date | Thu, 09 Mar 2006 22:53:18 +0000 |
parents | d7ddb3e565de |
children |
rev | line source |
---|---|
2543 | 1 ;;; loadhist.el --- lisp functions for working with feature groups |
14169 | 2 |
88155 | 3 ;; Copyright (C) 1995, 1998, 2000, 2002, 2003, 2004, |
4 ;; 2005 Free Software Foundation, Inc. | |
2543 | 5 |
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com> | |
29090
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
7 ;; Maintainer: FSF |
2543 | 8 ;; Keywords: internal |
9 | |
11289 | 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 | |
14169 | 23 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
88155 | 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02110-1301, USA. | |
11289 | 26 |
2543 | 27 ;;; Commentary: |
28 | |
29 ;; These functions exploit the load-history system variable. | |
22459
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
30 ;; Entry points include `unload-feature', `symbol-file', and |
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
31 ;; `feature-file', documented in the Emacs Lisp manual. |
2543 | 32 |
33 ;;; Code: | |
34 | |
35 (defun feature-symbols (feature) | |
47017
0ba62d75345e
(unload-feature): Distinguish functions from variables in load-history.
Richard M. Stallman <rms@gnu.org>
parents:
33094
diff
changeset
|
36 "Return the file and list of definitions associated with FEATURE. |
0ba62d75345e
(unload-feature): Distinguish functions from variables in load-history.
Richard M. Stallman <rms@gnu.org>
parents:
33094
diff
changeset
|
37 The value is actually the element of `load-history' |
0ba62d75345e
(unload-feature): Distinguish functions from variables in load-history.
Richard M. Stallman <rms@gnu.org>
parents:
33094
diff
changeset
|
38 for the file that did (provide FEATURE)." |
2543 | 39 (catch 'foundit |
29090
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
40 (mapc (lambda (x) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
41 (if (member (cons 'provide feature) (cdr x)) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
42 (throw 'foundit x))) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
43 load-history) |
2543 | 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 | |
29090
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
49 Lisp file without an extension. If the feature came from an `eval-buffer' on |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
50 a buffer with no associated file, or an `eval-region', return nil." |
2543 | 51 (if (not (featurep feature)) |
29090
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
52 (error "%S is not a currently loaded feature" feature) |
2543 | 53 (car (feature-symbols feature)))) |
54 | |
88155 | 55 (defun file-loadhist-lookup (file) |
56 "Return the `load-history' element for FILE. | |
57 FILE can be a file name, or a library name. | |
58 A library name is equivalent to the file name that `load-library' would load." | |
59 ;; First look for FILE as given. | |
60 (let ((symbols (assoc file load-history))) | |
61 ;; Try converting a library name to an absolute file name. | |
62 (and (null symbols) | |
63 (let ((absname | |
64 (locate-file file load-path load-suffixes))) | |
65 (and absname (not (equal absname file)) | |
66 (setq symbols (cdr (assoc absname load-history)))))) | |
67 symbols)) | |
68 | |
2543 | 69 (defun file-provides (file) |
88155 | 70 "Return the list of features provided by FILE as it was loaded. |
71 FILE can be a file name, or a library name. | |
72 A library name is equivalent to the file name that `load-library' would load." | |
73 (let ((symbols (file-loadhist-lookup file)) | |
29090
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
74 provides) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
75 (mapc (lambda (x) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
76 (if (and (consp x) (eq (car x) 'provide)) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
77 (setq provides (cons (cdr x) provides)))) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
78 symbols) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
79 provides)) |
2543 | 80 |
81 (defun file-requires (file) | |
88155 | 82 "Return the list of features required by FILE as it was loaded. |
83 FILE can be a file name, or a library name. | |
84 A library name is equivalent to the file name that `load-library' would load." | |
85 (let ((symbols (file-loadhist-lookup file)) | |
29090
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
86 requires) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
87 (mapc (lambda (x) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
88 (if (and (consp x) (eq (car x) 'require)) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
89 (setq requires (cons (cdr x) requires)))) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
90 symbols) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
91 requires)) |
2543 | 92 |
29090
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
93 (defsubst file-set-intersect (p q) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
94 "Return the set intersection of two lists." |
2543 | 95 (let ((ret nil)) |
29090
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
96 (dolist (x p ret) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
97 (if (memq x q) (setq ret (cons x ret)))) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
98 ret)) |
2543 | 99 |
100 (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
|
101 "Return the list of loaded libraries that depend on FILE. |
88155 | 102 This can include FILE itself. |
103 FILE can be a file name, or a library name. | |
104 A library name is equivalent to the file name that `load-library' would load." | |
29090
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
105 (let ((provides (file-provides file)) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
106 (dependents nil)) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
107 (dolist (x load-history dependents) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
108 (if (file-set-intersect provides (file-requires (car x))) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
109 (setq dependents (cons (car x) dependents)))) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
110 dependents)) |
2543 | 111 |
16173
9ba176963a26
(read-feature): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
112 (defun read-feature (prompt) |
22459
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
113 "Read a feature name \(string\) from the minibuffer. |
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
114 Prompt with PROMPT and completing from `features', and |
16173
9ba176963a26
(read-feature): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
115 return the feature \(symbol\)." |
9ba176963a26
(read-feature): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
116 (intern (completing-read prompt |
29090
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
117 (mapcar (lambda (feature) |
86d462e378ce
(feature-symbols, file-provides, file-requires): Use mapc.
Dave Love <fx@gnu.org>
parents:
23852
diff
changeset
|
118 (list (symbol-name feature))) |
16173
9ba176963a26
(read-feature): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
119 features) |
9ba176963a26
(read-feature): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
120 nil t))) |
9ba176963a26
(read-feature): New function.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
121 |
88155 | 122 (defvaralias 'loadhist-hook-functions 'unload-feature-special-hooks) |
123 (defvar unload-feature-special-hooks | |
29346
7ac9c66520d4
(loadhist-hook-functions): Remove before-change-function,
Dave Love <fx@gnu.org>
parents:
29241
diff
changeset
|
124 '(after-change-functions |
47017
0ba62d75345e
(unload-feature): Distinguish functions from variables in load-history.
Richard M. Stallman <rms@gnu.org>
parents:
33094
diff
changeset
|
125 after-insert-file-functions auto-fill-function |
0ba62d75345e
(unload-feature): Distinguish functions from variables in load-history.
Richard M. Stallman <rms@gnu.org>
parents:
33094
diff
changeset
|
126 before-change-functions blink-paren-function |
0ba62d75345e
(unload-feature): Distinguish functions from variables in load-history.
Richard M. Stallman <rms@gnu.org>
parents:
33094
diff
changeset
|
127 buffer-access-fontify-functions command-line-functions |
0ba62d75345e
(unload-feature): Distinguish functions from variables in load-history.
Richard M. Stallman <rms@gnu.org>
parents:
33094
diff
changeset
|
128 comment-indent-function kill-buffer-query-functions |
0ba62d75345e
(unload-feature): Distinguish functions from variables in load-history.
Richard M. Stallman <rms@gnu.org>
parents:
33094
diff
changeset
|
129 kill-emacs-query-functions lisp-indent-function |
0ba62d75345e
(unload-feature): Distinguish functions from variables in load-history.
Richard M. Stallman <rms@gnu.org>
parents:
33094
diff
changeset
|
130 mouse-position-function |
0ba62d75345e
(unload-feature): Distinguish functions from variables in load-history.
Richard M. Stallman <rms@gnu.org>
parents:
33094
diff
changeset
|
131 redisplay-end-trigger-functions temp-buffer-show-function |
0ba62d75345e
(unload-feature): Distinguish functions from variables in load-history.
Richard M. Stallman <rms@gnu.org>
parents:
33094
diff
changeset
|
132 window-scroll-functions window-size-change-functions |
0ba62d75345e
(unload-feature): Distinguish functions from variables in load-history.
Richard M. Stallman <rms@gnu.org>
parents:
33094
diff
changeset
|
133 write-region-annotate-functions) |
33094
51c874361c84
(unload-feature): Call elp-restore-function,
Dave Love <fx@gnu.org>
parents:
31674
diff
changeset
|
134 "A list of special hooks from Info node `(elisp)Standard Hooks'. |
22459
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 |
88155 | 140 (defvar unload-hook-features-list nil |
141 "List of features of the package being unloaded. | |
142 | |
143 This is meant to be used by FEATURE-unload-hook hooks, see the | |
144 documentation of `unload-feature' for details.") | |
145 | |
2543 | 146 ;;;###autoload |
147 (defun unload-feature (feature &optional force) | |
148 "Unload the library that provided FEATURE, restoring all its autoloads. | |
29241
a243b2d9c015
(unload-feature): Fix interactive spec [from
Dave Love <fx@gnu.org>
parents:
29090
diff
changeset
|
149 If the feature is required by any other loaded code, and prefix arg FORCE |
88155 | 150 is nil, raise an error. |
151 | |
152 This function tries to undo modifications made by the package to | |
153 hooks. Packages may define a hook FEATURE-unload-hook that is called | |
154 instead of the normal heuristics for doing this. Such a hook should | |
155 undo all the relevant global state changes that may have been made by | |
156 loading the package or executing functions in it. It has access to | |
157 the package's feature list (before anything is unbound) in the | |
158 variable `unload-hook-features-list' and could remove features from it | |
159 in the event that the package has done something normally-ill-advised, | |
160 such as redefining an Emacs function." | |
29241
a243b2d9c015
(unload-feature): Fix interactive spec [from
Dave Love <fx@gnu.org>
parents:
29090
diff
changeset
|
161 (interactive (list (read-feature "Feature: ") current-prefix-arg)) |
88155 | 162 (unless (featurep feature) |
163 (error "%s is not a currently loaded feature" (symbol-name feature))) | |
164 (unless force | |
165 (let* ((file (feature-file feature)) | |
166 (dependents (delete file (copy-sequence (file-dependents file))))) | |
167 (when dependents | |
168 (error "Loaded libraries %s depend on %s" | |
169 (prin1-to-string dependents) file)))) | |
170 (let* ((unload-hook-features-list (feature-symbols feature)) | |
171 (file (pop unload-hook-features-list)) | |
22459
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
172 (unload-hook (intern-soft (concat (symbol-name feature) |
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
173 "-unload-hook")))) |
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
174 ;; 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
|
175 ;; places go away. (Some packages install things on |
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
176 ;; `kill-buffer-hook', `activate-menubar-hook' and the like.) |
88155 | 177 ;; First off, provide a clean way for package FOO to arrange |
178 ;; this by adding hooks on the variable `FOO-unload-hook'. | |
22459
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
179 (if unload-hook |
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
180 (run-hooks unload-hook) |
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
181 ;; 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
|
182 ;; 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
|
183 ;; remove anything from them which matches the feature-symbols |
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
184 ;; 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
|
185 ;; functions which the package might just have installed, and |
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
186 ;; there might be other important state, but this tactic |
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
187 ;; normally works. |
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
188 (mapatoms |
bc88a299d2fc
(read-feature): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16173
diff
changeset
|
189 (lambda (x) |
88155 | 190 (when (and (boundp x) |
191 (or (and (consp (symbol-value x)) ; Random hooks. | |
192 (string-match "-hooks?\\'" (symbol-name x))) | |
193 (memq x unload-feature-special-hooks))) ; Known abnormal hooks etc. | |
194 (dolist (y unload-hook-features-list) | |
195 (when (and (eq (car-safe y) 'defun) | |
196 (not (get (cdr y) 'autoload))) | |
197 (remove-hook x (cdr y))))))) | |
198 ;; Remove any feature-symbols from auto-mode-alist as well. | |
199 (dolist (y unload-hook-features-list) | |
200 (when (and (eq (car-safe y) 'defun) | |
201 (not (get (cdr y) 'autoload))) | |
202 (setq auto-mode-alist | |
203 (rassq-delete-all (cdr y) auto-mode-alist))))) | |
204 (when (fboundp 'elp-restore-function) ; remove ELP stuff first | |
205 (dolist (elt unload-hook-features-list) | |
206 (when (symbolp elt) | |
207 (elp-restore-function elt)))) | |
208 (dolist (x unload-hook-features-list) | |
209 (if (consp x) | |
210 (cond | |
211 ;; Remove any feature names that this file provided. | |
212 ((eq (car x) 'provide) | |
213 (setq features (delq (cdr x) features))) | |
214 ((eq (car x) 'defun) | |
215 (let ((fun (cdr x))) | |
216 (when (fboundp fun) | |
217 (when (fboundp 'ad-unadvise) | |
218 (ad-unadvise fun)) | |
219 (fmakunbound fun) | |
220 (let ((aload (get fun 'autoload))) | |
221 (when aload | |
222 (fset fun (cons 'autoload aload)))))))) | |
223 ;; Kill local values as much as possible. | |
224 (dolist (buf (buffer-list)) | |
225 (with-current-buffer buf | |
226 (kill-local-variable x))) | |
227 ;; Get rid of the default binding if we can. | |
228 (unless (local-variable-if-set-p x) | |
229 (makunbound x)))) | |
12751
f442c14a79c4
(unload-feature): Delete the file's load-history element.
Richard M. Stallman <rms@gnu.org>
parents:
11844
diff
changeset
|
230 ;; Delete the load-history element for this file. |
88155 | 231 (setq load-history (delq (assoc file load-history) load-history)))) |
2543 | 232 |
233 (provide 'loadhist) | |
234 | |
88155 | 235 ;;; arch-tag: 70bb846a-c413-4f01-bf88-78dba4ac0798 |
2543 | 236 ;;; loadhist.el ends here |