1 ;;; loadhist.el --- lisp functions for working with feature groups
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Keywords: internal, dumped
9 ;; This file is part of XEmacs.
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)
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.
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
26 ;;; Synched up with: FSF 20.2.
30 ;; This file is dumped with XEmacs.
32 ;; These functions exploit the load-history system variable.
33 ;; Entry points include `unload-feature', `symbol-file', and `feature-file'.
37 ;; load-history is a list of entries that look like this:
38 ;; ("outline" outline-regexp ... (require . wid-edit) ... (provide . outline) ...)
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)))))
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))
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)))
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)))
73 (defun file-provides (file)
74 "Return the list of features provided by FILE."
76 (dolist (x (cdr (file-symbols file)))
77 (when (eq (car-safe x) 'provide)
78 (push (cdr x) provides)))
81 (defun file-requires (file)
82 "Return the list of features required by FILE."
84 (dolist (x (cdr (file-symbols file)))
85 (when (eq (car-safe x) 'require)
86 (push (cdr x) requires)))
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))
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))))
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)))
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)))
121 (let* ((file (feature-file feature))
122 (dependents (delete file (copy-sequence (file-dependents file)))))
124 (error "Loaded libraries %s depend on %s"
125 (prin1-to-string dependents) file))))
126 (let* ((flist (feature-symbols feature))
128 (flet ((reset-aload (x)
129 (let ((aload (get x 'autoload)))
130 (if aload (fset x (cons 'autoload aload))))))
133 (cond ((stringp x) nil)
135 ;; Remove any feature names that this file provided.
136 (if (eq (car x) 'provide)
137 (setq features (delq (cdr x) features))))
149 ;; Delete the load-history element for this file.
150 (let ((elt (assoc file load-history)))
151 (setq load-history (delq elt load-history)))))
155 ;;; loadhist.el ends here