Add copyright.
[bpt/emacs.git] / lisp / loadhist.el
CommitLineData
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.
16This 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.
35Actually, return the load argument, if any; this is sometimes the name of a
36Lisp file without an extension. If the feature came from an eval-buffer on
37a 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)
e01ac82c
RS
74 "Return the list of loaded libraries that depend on FILE.
75This can include FILE itself."
2b86d62c
RS
76 (let ((provides (file-provides file)) (dependents nil))
77 (mapcar
78 (function (lambda (x)
79 (if (set-intersect provides (file-requires (car x)))
80 (setq dependents (cons (car x) dependents)))))
81 load-history)
82 dependents
83 ))
84
85;;;###autoload
86(defun unload-feature (feature &optional force)
87 "Unload the library that provided FEATURE, restoring all its autoloads.
88If the feature is required by any other loaded code, and optional FORCE
89is nil, raise an error."
90 (interactive "SFeature: ")
91 (if (not (featurep feature))
92 (error "%s is not a currently loaded feature." (symbol-name feature)))
93 (if (not force)
e01ac82c
RS
94 (let* ((file (feature-file feature))
95 (dependents (delete file (copy-sequence (file-dependents file)))))
2b86d62c
RS
96 (if dependents
97 (error "Loaded libraries %s depend on %s."
98 (prin1-to-string dependents) file)
99 )))
100 (let* ((flist (feature-symbols feature)) (file (car flist)))
101 (mapcar
102 (function (lambda (x)
103 (cond ((stringp x) nil)
67fc28a7 104 ((consp x) nil)
2b86d62c
RS
105 ((boundp x) (makunbound x))
106 ((fboundp x)
107 (fmakunbound x)
108 (let ((aload (get x 'autoload)))
96c3ed60 109 (if aload (fset x (cons 'autoload aload)))))))
2b86d62c
RS
110 )
111 (cdr flist))))
112
113(provide 'loadhist)
114
115;;; loadhist.el ends here