Flymake fix to handle more C++ filenames (Bug#5488).
[emacs.git] / lisp / loadhist.el
blobb255a5d947bd2580f7bfa62ba22204e800b6bdcd
1 ;;; loadhist.el --- lisp functions for working with feature groups
3 ;; Copyright (C) 1995, 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Maintainer: FSF
8 ;; Keywords: internal
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; These functions exploit the load-history system variable.
28 ;; Entry points include `unload-feature', `symbol-file', and
29 ;; `feature-file', documented in the Emacs Lisp manual.
31 ;;; Code:
33 (eval-when-compile (require 'cl))
35 (defun feature-symbols (feature)
36 "Return the file and list of definitions associated with FEATURE.
37 The value is actually the element of `load-history'
38 for the file that did (provide FEATURE)."
39 (catch 'foundit
40 (let ((element (cons 'provide feature)))
41 (dolist (x load-history nil)
42 (when (member element (cdr x))
43 (throw 'foundit x))))))
45 (defun feature-file (feature)
46 "Return the file name from which a given FEATURE was loaded.
47 Actually, return the load argument, if any; this is sometimes the name of a
48 Lisp file without an extension. If the feature came from an `eval-buffer' on
49 a buffer with no associated file, or an `eval-region', return nil."
50 (if (not (featurep feature))
51 (error "%S is not a currently loaded feature" feature)
52 (car (feature-symbols feature))))
54 (defun file-loadhist-lookup (file)
55 "Return the `load-history' element for FILE.
56 FILE can be a file name, or a library name.
57 A library name is equivalent to the file name that `load-library' would load."
58 ;; First look for FILE as given.
59 (let ((symbols (assoc file load-history)))
60 ;; Try converting a library name to an absolute file name.
61 (and (null symbols)
62 (let ((absname
63 (locate-file file load-path (get-load-suffixes))))
64 (and absname (not (equal absname file))
65 (setq symbols (cdr (assoc absname load-history))))))
66 symbols))
68 (defun file-provides (file)
69 "Return the list of features provided by FILE as it was loaded.
70 FILE can be a file name, or a library name.
71 A library name is equivalent to the file name that `load-library' would load."
72 (let (provides)
73 (dolist (x (file-loadhist-lookup file) provides)
74 (when (eq (car-safe x) 'provide)
75 (push (cdr x) provides)))))
77 (defun file-requires (file)
78 "Return the list of features required by FILE as it was loaded.
79 FILE can be a file name, or a library name.
80 A library name is equivalent to the file name that `load-library' would load."
81 (let (requires)
82 (dolist (x (file-loadhist-lookup file) requires)
83 (when (eq (car-safe x) 'require)
84 (push (cdr x) requires)))))
86 (defsubst file-set-intersect (p q)
87 "Return the set intersection of two lists."
88 (let (ret)
89 (dolist (x p ret)
90 (when (memq x q) (push x ret)))))
92 (defun file-dependents (file)
93 "Return the list of loaded libraries that depend on FILE.
94 This can include FILE itself.
95 FILE can be a file name, or a library name.
96 A library name is equivalent to the file name that `load-library' would load."
97 (let ((provides (file-provides file))
98 (dependents nil))
99 (dolist (x load-history dependents)
100 (when (file-set-intersect provides (file-requires (car x)))
101 (push (car x) dependents)))))
103 (defun read-feature (prompt &optional loaded-p)
104 "Read feature name from the minibuffer, prompting with string PROMPT.
105 If optional second arg LOADED-P is non-nil, the feature must be loaded
106 from a file."
107 (intern
108 (completing-read prompt
109 (cons nil features)
110 (and loaded-p
111 #'(lambda (f)
112 (and f ; ignore nil
113 (feature-file f))))
114 loaded-p)))
116 (defvaralias 'loadhist-hook-functions 'unload-feature-special-hooks)
117 (defvar unload-feature-special-hooks
118 '(after-change-functions after-insert-file-functions
119 after-make-frame-functions auto-fill-function before-change-functions
120 blink-paren-function buffer-access-fontify-functions
121 choose-completion-string-functions command-line-functions
122 comment-indent-function compilation-finish-functions delete-frame-functions
123 disabled-command-function find-file-not-found-functions
124 font-lock-beginning-of-syntax-function font-lock-fontify-buffer-function
125 font-lock-fontify-region-function font-lock-mark-block-function
126 font-lock-syntactic-face-function font-lock-unfontify-buffer-function
127 font-lock-unfontify-region-function kill-buffer-query-functions
128 kill-emacs-query-functions lisp-indent-function mouse-position-function
129 redisplay-end-trigger-functions suspend-tty-functions
130 temp-buffer-show-function window-scroll-functions
131 window-size-change-functions write-contents-functions write-file-functions
132 write-region-annotate-functions)
133 "A list of special hooks from Info node `(elisp)Standard Hooks'.
135 These are symbols with hooklike values whose names don't end in
136 `-hook' or `-hooks', from which `unload-feature' should try to remove
137 pertinent symbols.")
139 (defvar unload-function-defs-list nil
140 "List of defintions in the Lisp library being unloaded.
142 This is meant to be used by `FEATURE-unload-function'; see the
143 documentation of `unload-feature' for details.")
144 (define-obsolete-variable-alias 'unload-hook-features-list
145 'unload-function-defs-list "22.2")
147 ;;;###autoload
148 (defun unload-feature (feature &optional force)
149 "Unload the library that provided FEATURE.
150 If the feature is required by any other loaded code, and prefix arg FORCE
151 is nil, raise an error.
153 Standard unloading activities include restoring old autoloads for
154 functions defined by the library, undoing any additions that the
155 library has made to hook variables or to `auto-mode-alist', undoing
156 ELP profiling of functions in that library, unproviding any features
157 provided by the library, and canceling timers held in variables
158 defined by the library.
160 If a function `FEATURE-unload-function' is defined, this function
161 calls it with no arguments, before doing anything else. That function
162 can do whatever is appropriate to undo the loading of the library. If
163 `FEATURE-unload-function' returns non-nil, that suppresses the
164 standard unloading of the library. Otherwise the standard unloading
165 proceeds.
167 `FEATURE-unload-function' has access to the package's list of
168 definitions in the variable `unload-function-defs-list' and could
169 remove symbols from it in the event that the package has done
170 something strange, such as redefining an Emacs function."
171 (interactive
172 (list
173 (read-feature "Unload feature: " t)
174 current-prefix-arg))
175 (unless (featurep feature)
176 (error "%s is not a currently loaded feature" (symbol-name feature)))
177 (unless force
178 (let* ((file (feature-file feature))
179 (dependents (delete file (copy-sequence (file-dependents file)))))
180 (when dependents
181 (error "Loaded libraries %s depend on %s"
182 (prin1-to-string dependents) file))))
183 (let* ((unload-function-defs-list (feature-symbols feature))
184 (file (pop unload-function-defs-list))
185 ;; If non-nil, this is a symbol for which we should
186 ;; restore a previous autoload if possible.
187 restore-autoload
188 (name (symbol-name feature))
189 (unload-hook (intern-soft (concat name "-unload-hook")))
190 (unload-func (intern-soft (concat name "-unload-function"))))
191 ;; If FEATURE-unload-function is defined and returns non-nil,
192 ;; don't try to do anything more; otherwise proceed normally.
193 (unless (and (fboundp unload-func)
194 (funcall unload-func))
195 ;; Try to avoid losing badly when hooks installed in critical
196 ;; places go away. (Some packages install things on
197 ;; `kill-buffer-hook', `activate-menubar-hook' and the like.)
198 (if unload-hook
199 ;; First off, provide a clean way for package FOO to arrange
200 ;; this by adding hooks on the variable `FOO-unload-hook'.
201 ;; This is obsolete; FEATURE-unload-function should be used now.
202 (run-hooks unload-hook)
203 ;; Otherwise, do our best. Look through the obarray for symbols
204 ;; which seem to be hook variables or special hook functions and
205 ;; remove anything from them which matches the feature-symbols
206 ;; about to get zapped. Obviously this won't get anonymous
207 ;; functions which the package might just have installed, and
208 ;; there might be other important state, but this tactic
209 ;; normally works.
210 (mapatoms
211 (lambda (x)
212 (when (and (boundp x)
213 (or (and (consp (symbol-value x)) ; Random hooks.
214 (string-match "-hooks?\\'" (symbol-name x)))
215 (memq x unload-feature-special-hooks))) ; Known abnormal hooks etc.
216 (dolist (y unload-function-defs-list)
217 (when (and (eq (car-safe y) 'defun)
218 (not (get (cdr y) 'autoload)))
219 (remove-hook x (cdr y)))))))
220 ;; Remove any feature-symbols from auto-mode-alist as well.
221 (dolist (y unload-function-defs-list)
222 (when (and (eq (car-safe y) 'defun)
223 (not (get (cdr y) 'autoload)))
224 (setq auto-mode-alist
225 (rassq-delete-all (cdr y) auto-mode-alist)))))
226 (when (fboundp 'elp-restore-function) ; remove ELP stuff first
227 (dolist (elt unload-function-defs-list)
228 (when (symbolp elt)
229 (elp-restore-function elt))))
231 (dolist (x unload-function-defs-list)
232 (if (consp x)
233 (case (car x)
234 ;; Remove any feature names that this file provided.
235 (provide
236 (setq features (delq (cdr x) features)))
237 ((defun autoload)
238 (let ((fun (cdr x)))
239 (when (fboundp fun)
240 (when (fboundp 'ad-unadvise)
241 (ad-unadvise fun))
242 (let ((aload (get fun 'autoload)))
243 (if (and aload (eq fun restore-autoload))
244 (fset fun (cons 'autoload aload))
245 (fmakunbound fun))))))
246 ;; (t . SYMBOL) comes before (defun . SYMBOL)
247 ;; and says we should restore SYMBOL's autoload
248 ;; when we undefine it.
249 ((t) (setq restore-autoload (cdr x)))
250 ((require defface) nil)
251 (t (message "Unexpected element %s in load-history" x)))
252 ;; Kill local values as much as possible.
253 (dolist (buf (buffer-list))
254 (with-current-buffer buf
255 (if (and (boundp x) (timerp (symbol-value x)))
256 (cancel-timer (symbol-value x)))
257 (kill-local-variable x)))
258 (if (and (boundp x) (timerp (symbol-value x)))
259 (cancel-timer (symbol-value x)))
260 ;; Get rid of the default binding if we can.
261 (unless (local-variable-if-set-p x)
262 (makunbound x))))
263 ;; Delete the load-history element for this file.
264 (setq load-history (delq (assoc file load-history) load-history))))
265 ;; Don't return load-history, it is not useful.
266 nil)
268 (provide 'loadhist)
270 ;; arch-tag: 70bb846a-c413-4f01-bf88-78dba4ac0798
271 ;;; loadhist.el ends here