(mail-interactive-insert-alias): Call mail-abbrev-expand-hook.
[bpt/emacs.git] / lisp / loadhist.el
CommitLineData
2b86d62c 1;;; loadhist.el --- lisp functions for working with feature groups
131a0c01 2;;; Copyright (C) 1995 Free Software Foundation, Inc.
2b86d62c
RS
3
4;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
5;; Version: 1.0
6;; Keywords: internal
7
131a0c01
KH
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
2b86d62c
RS
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.
33This 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.
52Actually, return the load argument, if any; this is sometimes the name of a
53Lisp file without an extension. If the feature came from an eval-buffer on
54a buffer with no associated file, or an eval-region, return nil."
55 (if (not (featurep feature))
a5c31fa1 56 (error "%s is not a currently loaded feature" (symbol-name feature))
2b86d62c
RS
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
a5c31fa1 81(defun file-set-intersect (p q)
2b86d62c
RS
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)
e01ac82c
RS
91 "Return the list of loaded libraries that depend on FILE.
92This can include FILE itself."
2b86d62c
RS
93 (let ((provides (file-provides file)) (dependents nil))
94 (mapcar
95 (function (lambda (x)
a5c31fa1 96 (if (file-set-intersect provides (file-requires (car x)))
2b86d62c
RS
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.
105If the feature is required by any other loaded code, and optional FORCE
106is nil, raise an error."
107 (interactive "SFeature: ")
108 (if (not (featurep feature))
a5c31fa1 109 (error "%s is not a currently loaded feature" (symbol-name feature)))
2b86d62c 110 (if (not force)
e01ac82c
RS
111 (let* ((file (feature-file feature))
112 (dependents (delete file (copy-sequence (file-dependents file)))))
2b86d62c 113 (if dependents
a5c31fa1 114 (error "Loaded libraries %s depend on %s"
2b86d62c
RS
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)
c344d4be
KH
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))))
2b86d62c
RS
125 ((boundp x) (makunbound x))
126 ((fboundp x)
127 (fmakunbound x)
128 (let ((aload (get x 'autoload)))
a5c31fa1
RS
129 (if aload (fset x (cons 'autoload aload))))))))
130 (cdr flist))
131 ;; Delete the load-history element for this file.
132 (let ((elt (assoc file load-history)))
133 (setq load-history (delq elt load-history)))))
2b86d62c
RS
134
135(provide 'loadhist)
136
137;;; loadhist.el ends here