(fill-region-as-paragraph): At end, advance over any newlines
[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)
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.
87If the feature is required by any other loaded code, and optional FORCE
88is 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)
67fc28a7 102 ((consp x) nil)
2b86d62c
RS
103 ((boundp x) (makunbound x))
104 ((fboundp x)
105 (fmakunbound x)
106 (let ((aload (get x 'autoload)))
96c3ed60 107 (if aload (fset x (cons 'autoload aload)))))))
2b86d62c
RS
108 )
109 (cdr flist))))
110
111(provide 'loadhist)
112
113;;; loadhist.el ends here