(GETTIMEOFDAY_ONE_ARGUMENT): Add #undef.
[bpt/emacs.git] / lisp / loadhist.el
1 ;;; loadhist.el --- lisp functions for working with feature groups
2 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
5 ;; Version: 1.0
6 ;; Keywords: internal
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; These functions exploit the load-history system variable.
27 ;; Entry points include `unload-feature', `symbol-file', and `feature-file'.
28
29 ;;; Code:
30
31 (defun symbol-file (sym)
32 "Return the input source from which SYM was loaded.
33 This is a file name, or nil if the source was a buffer with no associated file."
34 (catch 'foundit
35 (mapcar
36 (function (lambda (x) (if (memq sym (cdr x)) (throw 'foundit (car x)))))
37 load-history)
38 nil))
39
40 (defun feature-symbols (feature)
41 "Return the file and list of symbols associated with a given FEATURE."
42 (catch 'foundit
43 (mapcar
44 (function (lambda (x)
45 (if (member (cons 'provide feature) (cdr x))
46 (throw 'foundit x))))
47 load-history)
48 nil))
49
50 (defun feature-file (feature)
51 "Return the file name from which a given FEATURE was loaded.
52 Actually, return the load argument, if any; this is sometimes the name of a
53 Lisp file without an extension. If the feature came from an eval-buffer on
54 a buffer with no associated file, or an eval-region, return nil."
55 (if (not (featurep feature))
56 (error "%s is not a currently loaded feature." (symbol-name feature))
57 (car (feature-symbols feature))))
58
59 (defun file-provides (file)
60 "Return the list of features provided by FILE."
61 (let ((symbols (cdr (assoc file load-history))) (provides nil))
62 (mapcar
63 (function (lambda (x)
64 (if (and (consp x) (eq (car x) 'provide))
65 (setq provides (cons (cdr x) provides)))))
66 symbols)
67 provides
68 ))
69
70 (defun file-requires (file)
71 "Return the list of features required by FILE."
72 (let ((symbols (cdr (assoc file load-history))) (requires nil))
73 (mapcar
74 (function (lambda (x)
75 (if (and (consp x) (eq (car x) 'require))
76 (setq requires (cons (cdr x) requires)))))
77 symbols)
78 requires
79 ))
80
81 (defun set-intersect (p q)
82 ;; Return the set intersection of two lists
83 (let ((ret nil))
84 (mapcar
85 (function (lambda (x) (if (memq x q) (setq ret (cons x ret)))))
86 p)
87 ret
88 ))
89
90 (defun file-dependents (file)
91 "Return the list of loaded libraries that depend on FILE.
92 This can include FILE itself."
93 (let ((provides (file-provides file)) (dependents nil))
94 (mapcar
95 (function (lambda (x)
96 (if (set-intersect provides (file-requires (car x)))
97 (setq dependents (cons (car x) dependents)))))
98 load-history)
99 dependents
100 ))
101
102 ;;;###autoload
103 (defun unload-feature (feature &optional force)
104 "Unload the library that provided FEATURE, restoring all its autoloads.
105 If the feature is required by any other loaded code, and optional FORCE
106 is nil, raise an error."
107 (interactive "SFeature: ")
108 (if (not (featurep feature))
109 (error "%s is not a currently loaded feature." (symbol-name feature)))
110 (if (not force)
111 (let* ((file (feature-file feature))
112 (dependents (delete file (copy-sequence (file-dependents file)))))
113 (if dependents
114 (error "Loaded libraries %s depend on %s."
115 (prin1-to-string dependents) file)
116 )))
117 (let* ((flist (feature-symbols feature)) (file (car flist)))
118 (mapcar
119 (function (lambda (x)
120 (cond ((stringp x) nil)
121 ((consp x)
122 ;; Remove any feature names that this file provided.
123 (if (eq (car x) 'provide)
124 (setq features (delq (cdr x) features))))
125 ((boundp x) (makunbound x))
126 ((fboundp x)
127 (fmakunbound x)
128 (let ((aload (get x 'autoload)))
129 (if aload (fset x (cons 'autoload aload)))))))
130 )
131 (cdr flist))))
132
133 (provide 'loadhist)
134
135 ;;; loadhist.el ends here