update.
[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 ;; load-history is a list of entries that look like this:
38 ;; ("outline" outline-regexp ... (require . wid-edit) ... (provide . outline) ...)
39
40 (defun symbol-file (sym)
41   "Return the input source from which SYM was loaded.
42 This is a file name, or nil if the source was a buffer with no associated file."
43   (interactive "SFind source file for symbol: ") ; XEmacs
44   (dolist (entry load-history)
45     (when (memq sym (cdr entry))
46       (return (car entry)))))
47
48 (defun feature-symbols (feature)
49   "Return the file and list of symbols associated with a given FEATURE."
50   (let ((pair `(provide . ,feature)))
51     (dolist (entry load-history)
52       (when (member pair (cdr entry))
53         (return entry)))))
54
55 (defun feature-file (feature)
56   "Return the file name from which a given FEATURE was loaded.
57 Actually, return the load argument, if any; this is sometimes the name of a
58 Lisp file without an extension.  If the feature came from an eval-buffer on
59 a buffer with no associated file, or an eval-region, return nil."
60   (unless (featurep feature)
61     (error "%s is not a currently loaded feature" (symbol-name feature)))
62   (car (feature-symbols feature)))
63
64 (defun file-symbols (file)
65   "Return the file and list of symbols associated with FILE.
66 The file name in the returned list is the string used to load the file,
67 and may not be the same string as FILE, but it will be equivalent."
68   (or (assoc file load-history)
69       (assoc (file-name-sans-extension file) load-history)
70       (assoc (concat file ".el") load-history)
71       (assoc (concat file ".elc") load-history)))
72
73 (defun file-provides (file)
74   "Return the list of features provided by FILE."
75   (let ((provides nil))
76     (dolist (x (cdr (file-symbols file)))
77       (when (eq (car-safe x) 'provide)
78         (push (cdr x) provides)))
79     provides))
80
81 (defun file-requires (file)
82   "Return the list of features required by FILE."
83   (let ((requires nil))
84     (dolist (x (cdr (file-symbols file)))
85       (when (eq (car-safe x) 'require)
86         (push (cdr x) requires)))
87     requires))
88
89 (defun file-dependents (file)
90   "Return the list of loaded libraries that depend on FILE.
91 This can include FILE itself."
92   (let ((provides (file-provides file))
93         (dependents nil))
94     (dolist (entry load-history)
95       (dolist (x (cdr entry))
96         (when (and (eq (car-safe x) 'require)
97                    (memq (cdr-safe x) provides))
98           (push (car entry) dependents))))
99     dependents))
100
101 ;; FSFmacs
102 ;(defun read-feature (prompt)
103 ;  "Read a feature name \(string\) from the minibuffer,
104 ;prompting with PROMPT and completing from `features', and
105 ;return the feature \(symbol\)."
106 ;  (intern (completing-read prompt
107 ;                          (mapcar #'(lambda (feature)
108 ;                                    (list (symbol-name feature)))
109 ;                                  features)
110 ;                          nil t)))
111
112 ;; ;;;###autoload
113 (defun unload-feature (feature &optional force)
114   "Unload the library that provided FEATURE, restoring all its autoloads.
115 If the feature is required by any other loaded code, and optional FORCE
116 is nil, raise an error."
117   (interactive "SFeature: ")
118   (unless (featurep feature)
119     (error "%s is not a currently loaded feature" (symbol-name feature)))
120   (when (not force)
121     (let* ((file (feature-file feature))
122            (dependents (delete file (copy-sequence (file-dependents file)))))
123       (when dependents
124         (error "Loaded libraries %s depend on %s"
125                (prin1-to-string dependents) file))))
126   (let* ((flist (feature-symbols feature))
127          (file (car flist)))
128     (flet ((reset-aload (x)
129              (let ((aload (get x 'autoload)))
130                (if aload (fset x (cons 'autoload aload))))))
131     (mapcar
132      #'(lambda (x)
133          (cond ((stringp x) nil)
134                ((consp x)
135                 ;; Remove any feature names that this file provided.
136                 (if (eq (car x) 'provide)
137                     (setq features (delq (cdr x) features))))
138                ((and (boundp x)
139                      (fboundp x))
140                 (makunbound x)
141                 (fmakunbound x)
142                 (reset-aload x))
143                ((boundp x)
144                 (makunbound x))
145                ((fboundp x)
146                 (fmakunbound x)
147                 (reset-aload x))))
148      (cdr flist)))
149     ;; Delete the load-history element for this file.
150     (let ((elt (assoc file load-history)))
151       (setq load-history (delq elt load-history)))))
152
153 (provide 'loadhist)
154
155 ;;; loadhist.el ends here