Debug message fix
[sxemacs] / 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 SXEmacs.
10
11 ;; SXEmacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; SXEmacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Synched up with: FSF 20.2.
25
26 ;;; Commentary:
27
28 ;; This file is dumped with SXEmacs.
29
30 ;; These functions exploit the load-history system variable.
31 ;; Entry points include `unload-feature', `symbol-file', and `feature-file'.
32
33 ;;; Code:
34
35 ;; load-history is a list of entries that look like this:
36 ;; ("outline" outline-regexp ... (require . wid-edit) ... (provide . outline) ...)
37
38 ;; XEmacs; this function is in subr.el in GNU, and does not deal with
39 ;; built-in symbols.
40 (defun* symbol-file (sym &optional type)
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
44 If TYPE is nil or omitted, any kind of definition is acceptable.
45 If TYPE is `defun', then function, subr, special form or macro definitions
46 are acceptable.
47 If TYPE is `defvar', then variable definitions are acceptable.
48
49 `defface' specifies a face definition only, and for the moment, it won't
50 return faces created with `make-face' or `copy-face', just those created
51 with `defface' and `custom-declare-face'."
52   (interactive "SFind source file for symbol: ") ; XEmacs
53   (let (built-in-file autoload-cons symbol-details)
54     (cond ((and (eq 'autoload
55                     (car-safe
56                      (setq autoload-cons
57                            (and (fboundp sym) (symbol-function sym)))))
58                 (or (and (or (null type) (eq 'defvar type))
59                          (eq (fifth autoload-cons) 'keymap))
60                     (and (or (null type) (eq 'defun type))
61                          (memq (fifth autoload-cons) '(nil macro)))))
62            (return-from symbol-file (locate-library (second autoload-cons))))
63           ((eq 'defvar type)
64            ;; Load history entries corresponding to variables are just
65            ;; symbols.
66            (dolist (entry load-history)
67              (when (memq sym (cdr entry))
68                (return-from symbol-file (car entry)))))
69            ((not (null type))
70             ;; Non-variables have the type stored as the car of the entry.
71             (dolist (entry load-history)
72               (when (and (setq symbol-details (rassq sym (cdr entry)))
73                          (eq type (car symbol-details)))
74                 (return-from symbol-file (car entry)))))
75           (t
76            ;; If TYPE hasn't been specified, we need to check both for
77            ;; variables and other symbols.
78            (dolist (entry load-history)
79              (when (or (memq sym (cdr entry))
80                        (rassq sym (cdr entry)))
81                (return-from symbol-file (car entry))))))
82     (when (setq built-in-file (built-in-symbol-file sym type))
83       (if (equal built-in-file (file-truename built-in-file))
84           ;; Probably a full path name:
85           built-in-file
86         ;; This is a bit heuristic, but shouldn't realistically be a
87         ;; problem:
88         (if (string-match "\.elc?$" built-in-file)
89             (concat lisp-directory built-in-file)
90           (concat source-directory "/src/" built-in-file))))))
91
92 (defun feature-symbols (feature)
93   "Return the file and list of symbols associated with a given FEATURE."
94   (let ((pair `(provide . ,feature)))
95     (dolist (entry load-history)
96       (when (member pair (cdr entry))
97         (return entry)))))
98
99 (defun feature-file (feature)
100   "Return the file name from which a given FEATURE was loaded.
101 Actually, return the load argument, if any; this is sometimes the name of a
102 Lisp file without an extension.  If the feature came from an eval-buffer on
103 a buffer with no associated file, or an eval-region, return nil."
104   (unless (featurep feature)
105     (error "%s is not a currently loaded feature" (symbol-name feature)))
106   (car (feature-symbols feature)))
107
108 (defun file-symbols (file)
109   "Return the file and list of symbols associated with FILE.
110 The file name in the returned list is the string used to load the file,
111 and may not be the same string as FILE, but it will be equivalent."
112   (or (assoc file load-history)
113       (assoc (file-name-sans-extension file) load-history)
114       (assoc (concat file ".el") load-history)
115       (assoc (concat file ".elc") load-history)))
116
117 (defun file-provides (file)
118   "Return the list of features provided by FILE."
119   (let ((provides nil))
120     (dolist (x (cdr (file-symbols file)))
121       (when (eq (car-safe x) 'provide)
122         (push (cdr x) provides)))
123     provides))
124
125 (defun file-requires (file)
126   "Return the list of features required by FILE."
127   (let ((requires nil))
128     (dolist (x (cdr (file-symbols file)))
129       (when (eq (car-safe x) 'require)
130         (push (cdr x) requires)))
131     requires))
132
133 (defun file-dependents (file)
134   "Return the list of loaded libraries that depend on FILE.
135 This can include FILE itself."
136   (let ((provides (file-provides file))
137         (dependents nil))
138     (dolist (entry load-history)
139       (dolist (x (cdr entry))
140         (when (and (eq (car-safe x) 'require)
141                    (memq (cdr-safe x) provides))
142           (push (car entry) dependents))))
143     dependents))
144
145 ;; FSFmacs
146 ;(defun read-feature (prompt)
147 ;  "Read a feature name \(string\) from the minibuffer,
148 ;prompting with PROMPT and completing from `features', and
149 ;return the feature \(symbol\)."
150 ;  (intern (completing-read prompt
151 ;                          (mapcar #'(lambda (feature)
152 ;                                    (list (symbol-name feature)))
153 ;                                  features)
154 ;                          nil t)))
155
156 ;; ;;;###autoload
157 (defun unload-feature (feature &optional force)
158   "Unload the library that provided FEATURE, restoring all its autoloads.
159 If the feature is required by any other loaded code, and optional FORCE
160 is nil, raise an error."
161   (interactive "SFeature: ")
162   (unless (featurep feature)
163     (error "%s is not a currently loaded feature" (symbol-name feature)))
164   (when (not force)
165     (let* ((file (feature-file feature))
166            (dependents (delete file (copy-sequence (file-dependents file)))))
167       (when dependents
168         (error "Loaded libraries %s depend on %s"
169                (prin1-to-string dependents) file))))
170   (let* ((flist (feature-symbols feature))
171          (file (car flist)))
172     (flet ((reset-aload (x)
173              (let ((aload (get x 'autoload)))
174                (if aload (fset x (cons 'autoload aload))))))
175     (mapcar
176      #'(lambda (x)
177          (cond ((stringp x) nil)
178                ((consp x)
179                 ;; Remove any feature names that this file provided.
180                 (if (eq (car x) 'provide)
181                     (setq features (delq (cdr x) features))))
182                ((and (boundp x)
183                      (fboundp x))
184                 (makunbound x)
185                 (fmakunbound x)
186                 (reset-aload x))
187                ((boundp x)
188                 (makunbound x))
189                ((fboundp x)
190                 (fmakunbound x)
191                 (reset-aload x))))
192      (cdr flist)))
193     ;; Delete the load-history element for this file.
194     (let ((elt (assoc file load-history)))
195       (setq load-history (delq elt load-history)))))
196
197 (provide 'loadhist)
198
199 ;;; loadhist.el ends here