* src/gfilenotify.c (Fgfile_add_watch):
[emacs.git] / lisp / loadhist.el
blob52fd04736f62620bb38e00ba355bbeeb52a105b7
1 ;;; loadhist.el --- lisp functions for working with feature groups
3 ;; Copyright (C) 1995, 1998, 2000-2015 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs 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.
16 ;; GNU Emacs 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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; These functions exploit the load-history system variable.
27 ;; Entry points include `unload-feature', `symbol-file', and
28 ;; `feature-file', documented in the Emacs Lisp manual.
30 ;;; Code:
32 (defun feature-symbols (feature)
33 "Return the file and list of definitions associated with FEATURE.
34 The value is actually the element of `load-history'
35 for the file that did (provide FEATURE)."
36 (catch 'foundit
37 (let ((element (cons 'provide feature)))
38 (dolist (x load-history nil)
39 (when (member element (cdr x))
40 (throw 'foundit x))))))
42 (defun feature-file (feature)
43 "Return the file name from which a given FEATURE was loaded.
44 Actually, return the load argument, if any; this is sometimes the name of a
45 Lisp file without an extension. If the feature came from an `eval-buffer' on
46 a buffer with no associated file, or an `eval-region', return nil."
47 (if (not (featurep feature))
48 (error "%S is not a currently loaded feature" feature)
49 (car (feature-symbols feature))))
51 (defun file-loadhist-lookup (file)
52 "Return the `load-history' element for FILE.
53 FILE can be a file name, or a library name.
54 A library name is equivalent to the file name that `load-library' would load."
55 ;; First look for FILE as given.
56 (let ((symbols (assoc file load-history)))
57 ;; Try converting a library name to an absolute file name.
58 (and (null symbols)
59 (let ((absname
60 (locate-file file load-path (get-load-suffixes))))
61 (and absname (not (equal absname file))
62 (setq symbols (cdr (assoc absname load-history))))))
63 symbols))
65 (defun file-provides (file)
66 "Return the list of features provided by FILE as it was loaded.
67 FILE can be a file name, or a library name.
68 A library name is equivalent to the file name that `load-library' would load."
69 (let (provides)
70 (dolist (x (file-loadhist-lookup file) provides)
71 (when (eq (car-safe x) 'provide)
72 (push (cdr x) provides)))))
74 (defun file-requires (file)
75 "Return the list of features required by FILE as it was loaded.
76 FILE can be a file name, or a library name.
77 A library name is equivalent to the file name that `load-library' would load."
78 (let (requires)
79 (dolist (x (file-loadhist-lookup file) requires)
80 (when (eq (car-safe x) 'require)
81 (push (cdr x) requires)))))
83 (defsubst file-set-intersect (p q)
84 "Return the set intersection of two lists."
85 (let (ret)
86 (dolist (x p ret)
87 (when (memq x q) (push x ret)))))
89 (defun file-dependents (file)
90 "Return the list of loaded libraries that depend on FILE.
91 This can include FILE itself.
92 FILE can be a file name, or a library name.
93 A library name is equivalent to the file name that `load-library' would load."
94 (let ((provides (file-provides file))
95 (dependents nil))
96 (dolist (x load-history dependents)
97 (when (file-set-intersect provides (file-requires (car x)))
98 (push (car x) dependents)))))
100 (defun read-feature (prompt &optional loaded-p)
101 "Read feature name from the minibuffer, prompting with string PROMPT.
102 If optional second arg LOADED-P is non-nil, the feature must be loaded
103 from a file."
104 (intern (completing-read
105 prompt
106 (mapcar #'symbol-name
107 (if loaded-p
108 (delq nil
109 (mapcar
110 (lambda (x) (and (feature-file x) x))
111 features))
112 features)))))
114 (defvaralias 'loadhist-hook-functions 'unload-feature-special-hooks)
115 (defvar unload-feature-special-hooks
116 '(after-change-functions after-insert-file-functions
117 after-make-frame-functions auto-coding-functions
118 auto-fill-function before-change-functions
119 blink-paren-function buffer-access-fontify-functions
120 choose-completion-string-functions
121 comint-output-filter-functions command-line-functions
122 comment-indent-function compilation-finish-functions
123 delete-frame-functions disabled-command-function
124 fill-nobreak-predicate find-directory-functions
125 find-file-not-found-functions
126 font-lock-beginning-of-syntax-function
127 font-lock-fontify-buffer-function
128 font-lock-fontify-region-function
129 font-lock-mark-block-function
130 font-lock-syntactic-face-function
131 font-lock-unfontify-buffer-function
132 font-lock-unfontify-region-function
133 kill-buffer-query-functions kill-emacs-query-functions
134 lisp-indent-function mouse-position-function
135 redisplay-end-trigger-functions suspend-tty-functions
136 temp-buffer-show-function window-scroll-functions
137 window-size-change-functions write-contents-functions
138 write-file-functions write-region-annotate-functions)
139 "A list of special hooks from Info node `(elisp)Standard Hooks'.
141 These are symbols with hooklike values whose names don't end in
142 `-hook' or `-hooks', from which `unload-feature' should try to remove
143 pertinent symbols.")
145 (define-obsolete-variable-alias 'unload-hook-features-list
146 'unload-function-defs-list "22.2")
147 (defvar unload-function-defs-list nil
148 "List of definitions in the Lisp library being unloaded.
150 This is meant to be used by `FEATURE-unload-function'; see the
151 documentation of `unload-feature' for details.")
153 (defun unload--set-major-mode ()
154 (save-current-buffer
155 (dolist (buffer (buffer-list))
156 (set-buffer buffer)
157 (let ((proposed major-mode))
158 ;; Look for a predecessor mode not defined in the feature we're processing
159 (while (and proposed (rassq proposed unload-function-defs-list))
160 (setq proposed (get proposed 'derived-mode-parent)))
161 (unless (eq proposed major-mode)
162 ;; Two cases: either proposed is nil, and we want to switch to fundamental
163 ;; mode, or proposed is not nil and not major-mode, and so we use it.
164 (funcall (or proposed 'fundamental-mode)))))))
166 ;;;###autoload
167 (defun unload-feature (feature &optional force)
168 "Unload the library that provided FEATURE.
169 If the feature is required by any other loaded code, and prefix arg FORCE
170 is nil, raise an error.
172 Standard unloading activities include restoring old autoloads for
173 functions defined by the library, undoing any additions that the
174 library has made to hook variables or to `auto-mode-alist', undoing
175 ELP profiling of functions in that library, unproviding any features
176 provided by the library, and canceling timers held in variables
177 defined by the library.
179 If a function `FEATURE-unload-function' is defined, this function
180 calls it with no arguments, before doing anything else. That function
181 can do whatever is appropriate to undo the loading of the library. If
182 `FEATURE-unload-function' returns non-nil, that suppresses the
183 standard unloading of the library. Otherwise the standard unloading
184 proceeds.
186 `FEATURE-unload-function' has access to the package's list of
187 definitions in the variable `unload-function-defs-list' and could
188 remove symbols from it in the event that the package has done
189 something strange, such as redefining an Emacs function."
190 (interactive
191 (list
192 (read-feature "Unload feature: " t)
193 current-prefix-arg))
194 (unless (featurep feature)
195 (error "%s is not a currently loaded feature" (symbol-name feature)))
196 (unless force
197 (let* ((file (feature-file feature))
198 (dependents (delete file (copy-sequence (file-dependents file)))))
199 (when dependents
200 (error "Loaded libraries %s depend on %s"
201 (prin1-to-string dependents) file))))
202 (let* ((unload-function-defs-list (feature-symbols feature))
203 (file (pop unload-function-defs-list))
204 ;; If non-nil, this is a symbol for which we should
205 ;; restore a previous autoload if possible.
206 restore-autoload
207 (name (symbol-name feature))
208 (unload-hook (intern-soft (concat name "-unload-hook")))
209 (unload-func (intern-soft (concat name "-unload-function"))))
210 ;; If FEATURE-unload-function is defined and returns non-nil,
211 ;; don't try to do anything more; otherwise proceed normally.
212 (unless (and (fboundp unload-func)
213 (funcall unload-func))
214 ;; Try to avoid losing badly when hooks installed in critical
215 ;; places go away. (Some packages install things on
216 ;; `kill-buffer-hook', `activate-menubar-hook' and the like.)
217 (if unload-hook
218 ;; First off, provide a clean way for package FOO to arrange
219 ;; this by adding hooks on the variable `FOO-unload-hook'.
220 ;; This is obsolete; FEATURE-unload-function should be used now.
221 (run-hooks unload-hook)
222 ;; Otherwise, do our best. Look through the obarray for symbols
223 ;; which seem to be hook variables or special hook functions and
224 ;; remove anything from them which matches the feature-symbols
225 ;; about to get zapped. Obviously this won't get anonymous
226 ;; functions which the package might just have installed, and
227 ;; there might be other important state, but this tactic
228 ;; normally works.
229 (mapatoms
230 (lambda (x)
231 (when (and (boundp x)
232 (or (and (consp (symbol-value x)) ; Random hooks.
233 (string-match "-hooks?\\'" (symbol-name x)))
234 (memq x unload-feature-special-hooks))) ; Known abnormal hooks etc.
235 (dolist (y unload-function-defs-list)
236 (when (and (eq (car-safe y) 'defun)
237 (not (get (cdr y) 'autoload)))
238 (remove-hook x (cdr y)))))))
239 ;; Remove any feature-symbols from auto-mode-alist as well.
240 (dolist (y unload-function-defs-list)
241 (when (and (eq (car-safe y) 'defun)
242 (not (get (cdr y) 'autoload)))
243 (setq auto-mode-alist
244 (rassq-delete-all (cdr y) auto-mode-alist)))))
246 ;; Change major mode in all buffers using one defined in the feature being unloaded.
247 (unload--set-major-mode)
249 (when (fboundp 'elp-restore-function) ; remove ELP stuff first
250 (dolist (elt unload-function-defs-list)
251 (when (symbolp elt)
252 (elp-restore-function elt))))
254 (dolist (x unload-function-defs-list)
255 (if (consp x)
256 (pcase (car x)
257 ;; Remove any feature names that this file provided.
258 (`provide
259 (setq features (delq (cdr x) features)))
260 ((or `defun `autoload)
261 (let ((fun (cdr x)))
262 (when (fboundp fun)
263 (when (fboundp 'ad-unadvise)
264 (ad-unadvise fun))
265 (let ((aload (get fun 'autoload)))
266 (if (and aload (eq fun restore-autoload))
267 (fset fun (cons 'autoload aload))
268 (fmakunbound fun))))))
269 ;; (t . SYMBOL) comes before (defun . SYMBOL)
270 ;; and says we should restore SYMBOL's autoload
271 ;; when we undefine it.
272 (`t (setq restore-autoload (cdr x)))
273 ((or `require `defface) nil)
274 (_ (message "Unexpected element %s in load-history" x)))
275 ;; Kill local values as much as possible.
276 (dolist (buf (buffer-list))
277 (with-current-buffer buf
278 (if (and (boundp x) (timerp (symbol-value x)))
279 (cancel-timer (symbol-value x)))
280 (kill-local-variable x)))
281 (if (and (boundp x) (timerp (symbol-value x)))
282 (cancel-timer (symbol-value x)))
283 ;; Get rid of the default binding if we can.
284 (unless (local-variable-if-set-p x)
285 (makunbound x))))
286 ;; Delete the load-history element for this file.
287 (setq load-history (delq (assoc file load-history) load-history))))
288 ;; Don't return load-history, it is not useful.
289 nil)
291 (provide 'loadhist)
293 ;;; loadhist.el ends here