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