2021-04-09 11:44:44 +00:00
|
|
|
;;; loadhist.el --- lisp functions for working with feature groups -*- lexical-binding: t -*-
|
1996-01-14 07:34:30 +00:00
|
|
|
|
2022-01-01 07:45:51 +00:00
|
|
|
;; Copyright (C) 1995, 1998, 2000-2022 Free Software Foundation, Inc.
|
1993-04-16 23:43:57 +00:00
|
|
|
|
|
|
|
;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
|
2019-05-25 20:43:06 +00:00
|
|
|
;; Maintainer: emacs-devel@gnu.org
|
1993-04-16 23:43:57 +00:00
|
|
|
;; Keywords: internal
|
|
|
|
|
1995-04-08 02:24:20 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2008-05-06 08:06:51 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
1995-04-08 02:24:20 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 08:06:51 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
1995-04-08 02:24:20 +00:00
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
1995-04-08 02:24:20 +00:00
|
|
|
|
1993-04-16 23:43:57 +00:00
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; These functions exploit the load-history system variable.
|
1998-06-13 01:38:33 +00:00
|
|
|
;; Entry points include `unload-feature', `symbol-file', and
|
|
|
|
;; `feature-file', documented in the Emacs Lisp manual.
|
1993-04-16 23:43:57 +00:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
Explicitly require cl-lib where needed
Rather than relying on the byte-compiler happening to use it.
* lisp/completion.el, lisp/ffap.el, lisp/loadhist.el:
* lisp/userlock.el, lisp/emacs-lisp/debug.el, lisp/emacs-lisp/rx.el:
* lisp/emacs-lisp/testcover.el, lisp/mail/rfc2231.el:
* lisp/net/newst-treeview.el, lisp/net/puny.el:
* lisp/net/tramp-archive.el, lisp/net/tramp-gvfs.el:
* lisp/net/tramp-sh.el, lisp/net/tramp-smb.el, lisp/org/org-ctags.el:
* lisp/org/org-macs.el, lisp/progmodes/grep.el:
* lisp/progmodes/perl-mode.el, lisp/progmodes/ruby-mode.el:
* lisp/textmodes/dns-mode.el, lisp/textmodes/mhtml-mode.el:
* lisp/vc/pcvs-parse.el: Explicitly require cl-lib as needed.
2018-03-17 00:41:17 +00:00
|
|
|
(eval-when-compile (require 'cl-lib))
|
|
|
|
|
1993-04-16 23:43:57 +00:00
|
|
|
(defun feature-symbols (feature)
|
2002-08-24 03:00:29 +00:00
|
|
|
"Return the file and list of definitions associated with FEATURE.
|
|
|
|
The value is actually the element of `load-history'
|
|
|
|
for the file that did (provide FEATURE)."
|
2007-10-17 23:05:54 +00:00
|
|
|
(catch 'foundit
|
|
|
|
(let ((element (cons 'provide feature)))
|
|
|
|
(dolist (x load-history nil)
|
|
|
|
(when (member element (cdr x))
|
|
|
|
(throw 'foundit x))))))
|
1993-04-16 23:43:57 +00:00
|
|
|
|
|
|
|
(defun feature-file (feature)
|
|
|
|
"Return the file name from which a given FEATURE was loaded.
|
|
|
|
Actually, return the load argument, if any; this is sometimes the name of a
|
2000-05-22 19:35:34 +00:00
|
|
|
Lisp file without an extension. If the feature came from an `eval-buffer' on
|
|
|
|
a buffer with no associated file, or an `eval-region', return nil."
|
1993-04-16 23:43:57 +00:00
|
|
|
(if (not (featurep feature))
|
2000-05-22 19:35:34 +00:00
|
|
|
(error "%S is not a currently loaded feature" feature)
|
1993-04-16 23:43:57 +00:00
|
|
|
(car (feature-symbols feature))))
|
|
|
|
|
2005-01-02 23:43:51 +00:00
|
|
|
(defun file-loadhist-lookup (file)
|
2005-10-21 17:19:10 +00:00
|
|
|
"Return the `load-history' element for FILE.
|
|
|
|
FILE can be a file name, or a library name.
|
|
|
|
A library name is equivalent to the file name that `load-library' would load."
|
2005-01-02 23:43:51 +00:00
|
|
|
;; First look for FILE as given.
|
|
|
|
(let ((symbols (assoc file load-history)))
|
|
|
|
;; Try converting a library name to an absolute file name.
|
|
|
|
(and (null symbols)
|
2006-02-27 01:54:42 +00:00
|
|
|
(let ((absname
|
|
|
|
(locate-file file load-path (get-load-suffixes))))
|
2005-10-21 17:19:10 +00:00
|
|
|
(and absname (not (equal absname file))
|
|
|
|
(setq symbols (cdr (assoc absname load-history))))))
|
2005-01-02 23:43:51 +00:00
|
|
|
symbols))
|
|
|
|
|
1993-04-16 23:43:57 +00:00
|
|
|
(defun file-provides (file)
|
2005-10-21 17:19:10 +00:00
|
|
|
"Return the list of features provided by FILE as it was loaded.
|
|
|
|
FILE can be a file name, or a library name.
|
|
|
|
A library name is equivalent to the file name that `load-library' would load."
|
2007-10-17 23:05:54 +00:00
|
|
|
(let (provides)
|
|
|
|
(dolist (x (file-loadhist-lookup file) provides)
|
|
|
|
(when (eq (car-safe x) 'provide)
|
2008-01-25 22:50:33 +00:00
|
|
|
(push (cdr x) provides)))))
|
1993-04-16 23:43:57 +00:00
|
|
|
|
|
|
|
(defun file-requires (file)
|
2005-10-21 17:19:10 +00:00
|
|
|
"Return the list of features required by FILE as it was loaded.
|
|
|
|
FILE can be a file name, or a library name.
|
|
|
|
A library name is equivalent to the file name that `load-library' would load."
|
2007-10-17 23:05:54 +00:00
|
|
|
(let (requires)
|
|
|
|
(dolist (x (file-loadhist-lookup file) requires)
|
|
|
|
(when (eq (car-safe x) 'require)
|
2008-01-25 22:50:33 +00:00
|
|
|
(push (cdr x) requires)))))
|
2000-05-22 19:35:34 +00:00
|
|
|
|
1993-04-16 23:43:57 +00:00
|
|
|
(defun file-dependents (file)
|
1995-01-20 23:13:51 +00:00
|
|
|
"Return the list of loaded libraries that depend on FILE.
|
2005-10-21 17:19:10 +00:00
|
|
|
This can include FILE itself.
|
|
|
|
FILE can be a file name, or a library name.
|
|
|
|
A library name is equivalent to the file name that `load-library' would load."
|
2000-05-22 19:35:34 +00:00
|
|
|
(let ((provides (file-provides file))
|
|
|
|
(dependents nil))
|
|
|
|
(dolist (x load-history dependents)
|
2019-03-01 09:44:52 +00:00
|
|
|
(when (and (stringp (car x))
|
2021-04-04 23:13:54 +00:00
|
|
|
(seq-intersection provides (file-requires (car x)) #'eq))
|
2007-10-17 23:05:54 +00:00
|
|
|
(push (car x) dependents)))))
|
1993-04-16 23:43:57 +00:00
|
|
|
|
2007-04-06 17:03:19 +00:00
|
|
|
(defun read-feature (prompt &optional loaded-p)
|
|
|
|
"Read feature name from the minibuffer, prompting with string PROMPT.
|
|
|
|
If optional second arg LOADED-P is non-nil, the feature must be loaded
|
|
|
|
from a file."
|
2015-08-12 18:12:14 +00:00
|
|
|
(intern (completing-read
|
|
|
|
prompt
|
|
|
|
(mapcar #'symbol-name
|
|
|
|
(if loaded-p
|
|
|
|
(delq nil
|
|
|
|
(mapcar
|
|
|
|
(lambda (x) (and (feature-file x) x))
|
|
|
|
features))
|
|
|
|
features)))))
|
1996-09-12 18:31:16 +00:00
|
|
|
|
2003-10-05 13:50:18 +00:00
|
|
|
(defvaralias 'loadhist-hook-functions 'unload-feature-special-hooks)
|
|
|
|
(defvar unload-feature-special-hooks
|
2007-10-17 23:05:54 +00:00
|
|
|
'(after-change-functions after-insert-file-functions
|
2011-07-11 16:31:09 +00:00
|
|
|
after-make-frame-functions auto-coding-functions
|
|
|
|
auto-fill-function before-change-functions
|
2008-09-07 20:33:49 +00:00
|
|
|
blink-paren-function buffer-access-fontify-functions
|
2011-07-11 16:31:09 +00:00
|
|
|
choose-completion-string-functions
|
|
|
|
comint-output-filter-functions command-line-functions
|
|
|
|
comment-indent-function compilation-finish-functions
|
2011-06-08 07:17:26 +00:00
|
|
|
delete-frame-functions disabled-command-function
|
2011-07-11 16:31:09 +00:00
|
|
|
fill-nobreak-predicate find-directory-functions
|
|
|
|
find-file-not-found-functions
|
|
|
|
font-lock-fontify-buffer-function
|
|
|
|
font-lock-fontify-region-function
|
|
|
|
font-lock-mark-block-function
|
|
|
|
font-lock-syntactic-face-function
|
|
|
|
font-lock-unfontify-buffer-function
|
|
|
|
font-lock-unfontify-region-function
|
|
|
|
kill-buffer-query-functions kill-emacs-query-functions
|
|
|
|
lisp-indent-function mouse-position-function
|
2011-11-20 07:30:16 +00:00
|
|
|
redisplay-end-trigger-functions suspend-tty-functions
|
2011-07-11 16:31:09 +00:00
|
|
|
temp-buffer-show-function window-scroll-functions
|
|
|
|
window-size-change-functions write-contents-functions
|
|
|
|
write-file-functions write-region-annotate-functions)
|
2000-10-31 12:02:51 +00:00
|
|
|
"A list of special hooks from Info node `(elisp)Standard Hooks'.
|
1998-06-13 01:38:33 +00:00
|
|
|
|
2007-10-24 08:12:40 +00:00
|
|
|
These are symbols with hooklike values whose names don't end in
|
|
|
|
`-hook' or `-hooks', from which `unload-feature' should try to remove
|
1998-06-13 01:38:33 +00:00
|
|
|
pertinent symbols.")
|
|
|
|
|
2007-10-24 08:12:40 +00:00
|
|
|
(defvar unload-function-defs-list nil
|
2011-11-17 09:09:20 +00:00
|
|
|
"List of definitions in the Lisp library being unloaded.
|
2004-02-16 17:50:32 +00:00
|
|
|
|
2007-10-24 08:12:40 +00:00
|
|
|
This is meant to be used by `FEATURE-unload-function'; see the
|
2004-02-16 17:50:32 +00:00
|
|
|
documentation of `unload-feature' for details.")
|
|
|
|
|
2011-06-25 17:42:18 +00:00
|
|
|
(defun unload--set-major-mode ()
|
|
|
|
(save-current-buffer
|
|
|
|
(dolist (buffer (buffer-list))
|
|
|
|
(set-buffer buffer)
|
|
|
|
(let ((proposed major-mode))
|
2011-12-15 22:14:12 +00:00
|
|
|
;; Look for a predecessor mode not defined in the feature we're processing
|
2011-06-25 17:42:18 +00:00
|
|
|
(while (and proposed (rassq proposed unload-function-defs-list))
|
|
|
|
(setq proposed (get proposed 'derived-mode-parent)))
|
|
|
|
(unless (eq proposed major-mode)
|
|
|
|
;; Two cases: either proposed is nil, and we want to switch to fundamental
|
|
|
|
;; mode, or proposed is not nil and not major-mode, and so we use it.
|
|
|
|
(funcall (or proposed 'fundamental-mode)))))))
|
|
|
|
|
2022-01-31 16:07:06 +00:00
|
|
|
(defvar loadhist-unload-filename nil)
|
|
|
|
|
2017-07-24 17:38:17 +00:00
|
|
|
(cl-defgeneric loadhist-unload-element (x)
|
2022-01-31 16:07:06 +00:00
|
|
|
"Unload an element from the `load-history'.
|
|
|
|
The variable `loadhist-unload-filename' holds the name of the file we're
|
|
|
|
unloading."
|
2017-07-24 17:38:17 +00:00
|
|
|
(message "Unexpected element %S in load-history" x))
|
|
|
|
|
|
|
|
(cl-defmethod loadhist-unload-element ((x (head defun)))
|
2022-01-31 16:07:06 +00:00
|
|
|
(let* ((fun (cdr x))
|
|
|
|
(hist (get fun 'function-history)))
|
|
|
|
(cond
|
|
|
|
((null hist)
|
|
|
|
(defalias fun nil)
|
|
|
|
;; Override the change that `defalias' just recorded.
|
|
|
|
(put fun 'function-history nil))
|
|
|
|
((equal (car hist) loadhist-unload-filename)
|
|
|
|
(defalias fun (cadr hist))
|
|
|
|
;; Set the history afterwards, to override the change that
|
|
|
|
;; `defalias' records otherwise.
|
|
|
|
(put fun 'function-history (cddr hist)))
|
|
|
|
(t
|
|
|
|
;; Unloading a file whose definition is "inactive" (i.e. has been
|
|
|
|
;; overridden by another file): just remove it from the history,
|
|
|
|
;; so future unloading of that other file has a chance to DTRT.
|
|
|
|
(let* ((tmp (plist-member hist loadhist-unload-filename))
|
|
|
|
(pos (- (length hist) (length tmp))))
|
|
|
|
(cl-assert (> pos 1))
|
|
|
|
(setcdr (nthcdr (- pos 2) hist) (cdr tmp)))))))
|
2017-07-24 17:38:17 +00:00
|
|
|
|
2017-07-24 19:58:30 +00:00
|
|
|
(cl-defmethod loadhist-unload-element ((_ (head require))) nil)
|
|
|
|
(cl-defmethod loadhist-unload-element ((_ (head defface))) nil)
|
2017-07-24 17:38:17 +00:00
|
|
|
|
|
|
|
(cl-defmethod loadhist-unload-element ((x (head provide)))
|
|
|
|
;; Remove any feature names that this file provided.
|
|
|
|
(setq features (delq (cdr x) features)))
|
|
|
|
|
|
|
|
(cl-defmethod loadhist-unload-element ((x symbol))
|
|
|
|
;; Kill local values as much as possible.
|
|
|
|
(dolist (buf (buffer-list))
|
|
|
|
(with-current-buffer buf
|
|
|
|
(if (and (boundp x) (timerp (symbol-value x)))
|
|
|
|
(cancel-timer (symbol-value x)))
|
|
|
|
(kill-local-variable x)))
|
|
|
|
(if (and (boundp x) (timerp (symbol-value x)))
|
|
|
|
(cancel-timer (symbol-value x)))
|
|
|
|
;; Get rid of the default binding if we can.
|
|
|
|
(unless (local-variable-if-set-p x)
|
|
|
|
(makunbound x)))
|
|
|
|
|
|
|
|
(cl-defmethod loadhist-unload-element ((x (head define-type)))
|
2017-07-24 19:58:30 +00:00
|
|
|
(let* ((name (cdr x)))
|
2017-07-24 17:38:17 +00:00
|
|
|
;; Remove the struct.
|
|
|
|
(setf (cl--find-class name) nil)))
|
|
|
|
|
2017-07-28 16:02:01 +00:00
|
|
|
(cl-defmethod loadhist-unload-element ((x (head define-symbol-props)))
|
|
|
|
(pcase-dolist (`(,symbol . ,props) (cdr x))
|
|
|
|
(dolist (prop props)
|
|
|
|
(put symbol prop nil))))
|
|
|
|
|
1993-04-16 23:43:57 +00:00
|
|
|
;;;###autoload
|
|
|
|
(defun unload-feature (feature &optional force)
|
2007-10-24 08:12:40 +00:00
|
|
|
"Unload the library that provided FEATURE.
|
2000-05-26 11:06:05 +00:00
|
|
|
If the feature is required by any other loaded code, and prefix arg FORCE
|
2004-02-16 17:39:38 +00:00
|
|
|
is nil, raise an error.
|
|
|
|
|
2007-10-24 08:12:40 +00:00
|
|
|
Standard unloading activities include restoring old autoloads for
|
2020-04-06 15:05:33 +00:00
|
|
|
functions defined by the library, removing such functions from
|
|
|
|
hooks and `auto-mode-alist', undoing their ELP profiling,
|
|
|
|
unproviding any features provided by the library, and canceling
|
|
|
|
timers held in variables defined by the library.
|
2007-10-24 08:12:40 +00:00
|
|
|
|
|
|
|
If a function `FEATURE-unload-function' is defined, this function
|
|
|
|
calls it with no arguments, before doing anything else. That function
|
|
|
|
can do whatever is appropriate to undo the loading of the library. If
|
|
|
|
`FEATURE-unload-function' returns non-nil, that suppresses the
|
|
|
|
standard unloading of the library. Otherwise the standard unloading
|
|
|
|
proceeds.
|
|
|
|
|
|
|
|
`FEATURE-unload-function' has access to the package's list of
|
|
|
|
definitions in the variable `unload-function-defs-list' and could
|
|
|
|
remove symbols from it in the event that the package has done
|
|
|
|
something strange, such as redefining an Emacs function."
|
2007-04-06 17:03:19 +00:00
|
|
|
(interactive
|
|
|
|
(list
|
2007-04-06 17:05:39 +00:00
|
|
|
(read-feature "Unload feature: " t)
|
|
|
|
current-prefix-arg))
|
2005-04-19 15:09:05 +00:00
|
|
|
(unless (featurep feature)
|
|
|
|
(error "%s is not a currently loaded feature" (symbol-name feature)))
|
|
|
|
(unless force
|
|
|
|
(let* ((file (feature-file feature))
|
|
|
|
(dependents (delete file (copy-sequence (file-dependents file)))))
|
|
|
|
(when dependents
|
|
|
|
(error "Loaded libraries %s depend on %s"
|
|
|
|
(prin1-to-string dependents) file))))
|
2007-10-24 08:12:40 +00:00
|
|
|
(let* ((unload-function-defs-list (feature-symbols feature))
|
|
|
|
(file (pop unload-function-defs-list))
|
2022-01-31 16:07:06 +00:00
|
|
|
(loadhist-unload-filename file)
|
2007-10-19 09:05:52 +00:00
|
|
|
(name (symbol-name feature))
|
|
|
|
(unload-hook (intern-soft (concat name "-unload-hook")))
|
|
|
|
(unload-func (intern-soft (concat name "-unload-function"))))
|
|
|
|
;; If FEATURE-unload-function is defined and returns non-nil,
|
|
|
|
;; don't try to do anything more; otherwise proceed normally.
|
2007-10-30 09:44:17 +00:00
|
|
|
(unless (and (fboundp unload-func)
|
2007-10-19 09:05:52 +00:00
|
|
|
(funcall unload-func))
|
|
|
|
;; Try to avoid losing badly when hooks installed in critical
|
|
|
|
;; places go away. (Some packages install things on
|
|
|
|
;; `kill-buffer-hook', `activate-menubar-hook' and the like.)
|
|
|
|
(if unload-hook
|
|
|
|
;; First off, provide a clean way for package FOO to arrange
|
|
|
|
;; this by adding hooks on the variable `FOO-unload-hook'.
|
|
|
|
;; This is obsolete; FEATURE-unload-function should be used now.
|
|
|
|
(run-hooks unload-hook)
|
|
|
|
;; Otherwise, do our best. Look through the obarray for symbols
|
|
|
|
;; which seem to be hook variables or special hook functions and
|
|
|
|
;; remove anything from them which matches the feature-symbols
|
|
|
|
;; about to get zapped. Obviously this won't get anonymous
|
|
|
|
;; functions which the package might just have installed, and
|
|
|
|
;; there might be other important state, but this tactic
|
|
|
|
;; normally works.
|
2020-04-06 11:25:41 +00:00
|
|
|
(let ((removables (cl-loop for def in unload-function-defs-list
|
|
|
|
when (and (eq (car-safe def) 'defun)
|
|
|
|
(not (get (cdr def) 'autoload)))
|
|
|
|
collect (cdr def))))
|
|
|
|
(mapatoms
|
|
|
|
(lambda (x)
|
|
|
|
(when (and (boundp x)
|
|
|
|
(or (and (consp (symbol-value x)) ; Random hooks.
|
|
|
|
(string-match "-hooks?\\'" (symbol-name x)))
|
|
|
|
;; Known abnormal hooks etc.
|
|
|
|
(memq x unload-feature-special-hooks)))
|
|
|
|
(dolist (func removables)
|
|
|
|
(remove-hook x func)))))
|
2020-04-06 11:30:11 +00:00
|
|
|
(save-current-buffer
|
|
|
|
(dolist (buffer (buffer-list))
|
|
|
|
(pcase-dolist (`(,sym . ,val) (buffer-local-variables buffer))
|
|
|
|
(when (or (and (consp val)
|
|
|
|
(string-match "-hooks?\\'" (symbol-name sym)))
|
|
|
|
(memq sym unload-feature-special-hooks))
|
|
|
|
(set-buffer buffer)
|
|
|
|
(dolist (func removables)
|
|
|
|
(remove-hook sym func t))))))
|
2020-04-06 11:25:41 +00:00
|
|
|
;; Remove any feature-symbols from auto-mode-alist as well.
|
|
|
|
(dolist (func removables)
|
|
|
|
(setq auto-mode-alist
|
|
|
|
(rassq-delete-all func auto-mode-alist)))))
|
2011-06-25 17:42:18 +00:00
|
|
|
|
|
|
|
;; Change major mode in all buffers using one defined in the feature being unloaded.
|
|
|
|
(unload--set-major-mode)
|
|
|
|
|
2017-07-24 17:38:17 +00:00
|
|
|
(mapc #'loadhist-unload-element unload-function-defs-list)
|
2007-10-19 09:05:52 +00:00
|
|
|
;; Delete the load-history element for this file.
|
|
|
|
(setq load-history (delq (assoc file load-history) load-history))))
|
2007-02-05 23:45:31 +00:00
|
|
|
;; Don't return load-history, it is not useful.
|
|
|
|
nil)
|
1993-04-16 23:43:57 +00:00
|
|
|
|
2021-04-04 23:13:54 +00:00
|
|
|
;; Obsolete.
|
|
|
|
|
|
|
|
(defsubst file-set-intersect (p q)
|
|
|
|
"Return the set intersection of two lists."
|
|
|
|
(declare (obsolete seq-intersection "28.1"))
|
|
|
|
(nreverse (seq-intersection p q #'eq)))
|
|
|
|
|
1993-04-16 23:43:57 +00:00
|
|
|
(provide 'loadhist)
|
|
|
|
|
|
|
|
;;; loadhist.el ends here
|