Commit | Line | Data |
---|---|---|
2b86d62c RS |
1 | ;;; loadhist.el --- lisp functions for working with feature groups |
2 | ||
3 | ;; Author: Eric S. Raymond <esr@snark.thyrsus.com> | |
4 | ;; Version: 1.0 | |
5 | ;; Keywords: internal | |
6 | ||
7 | ;;; Commentary: | |
8 | ||
9 | ;; These functions exploit the load-history system variable. | |
10 | ;; Entry points include `unload-feature', `symbol-file', and `feature-file'. | |
11 | ||
12 | ;;; Code: | |
13 | ||
14 | (defun symbol-file (sym) | |
15 | "Return the input source from which SYM was loaded. | |
16 | This is a file name, or nil if the source was a buffer with no associated file." | |
17 | (catch 'foundit | |
18 | (mapcar | |
19 | (function (lambda (x) (if (memq sym (cdr x)) (throw 'foundit (car x))))) | |
20 | load-history) | |
21 | nil)) | |
22 | ||
23 | (defun feature-symbols (feature) | |
24 | "Return the file and list of symbols associated with a given FEATURE." | |
25 | (catch 'foundit | |
26 | (mapcar | |
27 | (function (lambda (x) | |
28 | (if (member (cons 'provide feature) (cdr x)) | |
29 | (throw 'foundit x)))) | |
30 | load-history) | |
31 | nil)) | |
32 | ||
33 | (defun feature-file (feature) | |
34 | "Return the file name from which a given FEATURE was loaded. | |
35 | Actually, return the load argument, if any; this is sometimes the name of a | |
36 | Lisp file without an extension. If the feature came from an eval-buffer on | |
37 | a buffer with no associated file, or an eval-region, return nil." | |
38 | (if (not (featurep feature)) | |
39 | (error "%s is not a currently loaded feature." (symbol-name feature)) | |
40 | (car (feature-symbols feature)))) | |
41 | ||
42 | (defun file-provides (file) | |
43 | "Return the list of features provided by FILE." | |
44 | (let ((symbols (cdr (assoc file load-history))) (provides nil)) | |
45 | (mapcar | |
46 | (function (lambda (x) | |
47 | (if (and (consp x) (eq (car x) 'provide)) | |
48 | (setq provides (cons (cdr x) provides))))) | |
49 | symbols) | |
50 | provides | |
51 | )) | |
52 | ||
53 | (defun file-requires (file) | |
54 | "Return the list of features required by FILE." | |
55 | (let ((symbols (cdr (assoc file load-history))) (requires nil)) | |
56 | (mapcar | |
57 | (function (lambda (x) | |
58 | (if (and (consp x) (eq (car x) 'require)) | |
59 | (setq requires (cons (cdr x) requires))))) | |
60 | symbols) | |
61 | requires | |
62 | )) | |
63 | ||
64 | (defun set-intersect (p q) | |
65 | ;; Return the set intersection of two lists | |
66 | (let ((ret nil)) | |
67 | (mapcar | |
68 | (function (lambda (x) (if (memq x q) (setq ret (cons x ret))))) | |
69 | p) | |
70 | ret | |
71 | )) | |
72 | ||
73 | (defun file-dependents (file) | |
74 | ;; Return the list of loaded libraries that depend on FILE. | |
75 | (let ((provides (file-provides file)) (dependents nil)) | |
76 | (mapcar | |
77 | (function (lambda (x) | |
78 | (if (set-intersect provides (file-requires (car x))) | |
79 | (setq dependents (cons (car x) dependents))))) | |
80 | load-history) | |
81 | dependents | |
82 | )) | |
83 | ||
84 | ;;;###autoload | |
85 | (defun unload-feature (feature &optional force) | |
86 | "Unload the library that provided FEATURE, restoring all its autoloads. | |
87 | If the feature is required by any other loaded code, and optional FORCE | |
88 | is nil, raise an error." | |
89 | (interactive "SFeature: ") | |
90 | (if (not (featurep feature)) | |
91 | (error "%s is not a currently loaded feature." (symbol-name feature))) | |
92 | (if (not force) | |
93 | (let* ((file (feature-file feature)) (dependents (file-dependents file))) | |
94 | (if dependents | |
95 | (error "Loaded libraries %s depend on %s." | |
96 | (prin1-to-string dependents) file) | |
97 | ))) | |
98 | (let* ((flist (feature-symbols feature)) (file (car flist))) | |
99 | (mapcar | |
100 | (function (lambda (x) | |
101 | (cond ((stringp x) nil) | |
102 | ((boundp x) (makunbound x)) | |
103 | ((fboundp x) | |
104 | (fmakunbound x) | |
105 | (let ((aload (get x 'autoload))) | |
106 | (if aload (fset x aload)))))) | |
107 | ) | |
108 | (cdr flist)))) | |
109 | ||
110 | (provide 'loadhist) | |
111 | ||
112 | ;;; loadhist.el ends here |