Pass operation to Ffind_file_name_handler.
[emacs.git] / lisp / loadhist.el
blob8f29c5a8a71a05d7177131122eff7c69ad4063d3
1 ;;; loadhist.el --- lisp functions for working with feature groups
3 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
4 ;; Version: 1.0
5 ;; Keywords: internal
7 ;;; Commentary:
9 ;; These functions exploit the load-history system variable.
10 ;; Entry points include `unload-feature', `symbol-file', and `feature-file'.
12 ;;; Code:
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))
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))
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))))
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
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
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)))))
70 ret
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
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 ((consp x) nil)
103 ((boundp x) (makunbound x))
104 ((fboundp x)
105 (fmakunbound x)
106 (let ((aload (get x 'autoload)))
107 (if aload (fset x aload))))))
109 (cdr flist))))
111 (provide 'loadhist)
113 ;;; loadhist.el ends here