From 6d34dcc9de1f99c795d0a6bcaa416e29c8500eed Mon Sep 17 00:00:00 2001 From: Pengji Zhang Date: Sun, 10 Nov 2024 09:30:01 +0800 Subject: [PATCH] Add option 'elisp-flymake-byte-compile-activate-packages' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This option controls whether the Flymake Emacs Lisp byte-compiler should activate user installed packages before checking the source buffer. (Bug#65035) * lisp/progmodes/elisp-mode.el (elisp-flymake-byte-compile-user-file-p): New predicate function to check if a buffer is visiting a user file. (elisp-flymake-byte-compile-activate-packages): New option. (elisp-flymake--byte-compile-activate-packages): New variable for caching. (elisp-flymake-byte-compile): Use the new option. * etc/NEWS: Announce the new option. Co-authored-by: João Távora --- etc/NEWS | 7 ++++++ lisp/progmodes/elisp-mode.el | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/etc/NEWS b/etc/NEWS index a6c2c895985..090f4293c8e 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -413,6 +413,13 @@ This affects calls to 'warn', 'lwarn', 'display-warning', and In most cases, having it enabled leads to a large amount of false positives. +--- +*** New user option 'elisp-flymake-byte-compile-activate-packages'. +This option controls whether or not the Flymake byte-compiler backend +should activate user installed packages before compiling the source +buffer. By default, it is set to activate packages when checking user +configuration files. Set it to nil to restore the previous behavior. + ** DocView --- diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el index 2f931daedc7..3905300c9a8 100644 --- a/lisp/progmodes/elisp-mode.el +++ b/lisp/progmodes/elisp-mode.el @@ -2190,6 +2190,44 @@ elisp-flymake-byte-compile-load-path (defvar bytecomp--inhibit-lexical-cookie-warning) +(defun elisp-flymake-byte-compile-user-file-p (buffer) + "Return non-nil if BUFFER is visiting a user file. +That means either the file is `user-init-file' or it is in +`user-emacs-directory'." + (when-let* ((file (buffer-local-value 'buffer-file-truename buffer))) + (or (and user-emacs-directory + (file-in-directory-p file user-emacs-directory)) + (and user-init-file + (string= file (abbreviate-file-name + (file-truename user-init-file))))))) + +(defcustom elisp-flymake-byte-compile-activate-packages + #'elisp-flymake-byte-compile-user-file-p + "Whether to activate packages for Flymake elisp byte-compilation. +If the value is nil, do not activate installed packages. If the value +is a function, it is called with one argument, the source buffer to be +checked, and installed packages are activated if the function returns +non-nil. Otherwise, packages are always activated. + +The default value is a predicate function +`elisp-flymake-byte-compile-user-file-p' (which see), and that means +packages are activated only for user configuration files. + +Note that for efficiency the return value of the predicate function is +cached the first time it is called. Type \\[revert-buffer-quick] to +invalidate the cached value." + :type '(choice + (const :tag "Don't activate" nil) + (const :tag "Always activate" t) + (const :tag "Activate for user files" + elisp-flymake-byte-compile-user-file-p) + (function :tag "Predicate function")) + :group 'lisp + :version "31.1") + +(defvar-local elisp-flymake--byte-compile-activate-packages :unset + "Cached value for `elisp-flymake-byte-compile-activate-packages'.") + ;;;###autoload (defun elisp-flymake-byte-compile (report-fn &rest _args) "A Flymake backend for elisp byte compilation. @@ -2205,6 +2243,12 @@ elisp-flymake-byte-compile (save-restriction (widen) (write-region (point-min) (point-max) temp-file nil 'nomessage)) + (when (eq elisp-flymake--byte-compile-activate-packages :unset) + (setq elisp-flymake--byte-compile-activate-packages + (if (functionp elisp-flymake-byte-compile-activate-packages) + (funcall elisp-flymake-byte-compile-activate-packages + source-buffer) + elisp-flymake-byte-compile-activate-packages))) (let* ((output-buffer (generate-new-buffer " *elisp-flymake-byte-compile*")) ;; Hack: suppress warning about missing lexical cookie in ;; *scratch* buffers. @@ -2223,6 +2267,8 @@ elisp-flymake-byte-compile ;; "--eval" "(setq load-prefer-newer t)" ; for testing ,@(mapcan (lambda (path) (list "-L" path)) elisp-flymake-byte-compile-load-path) + ,@(when elisp-flymake--byte-compile-activate-packages + '("-f" "package-activate-all")) ,@warning-suppression-opt "-f" "elisp-flymake--batch-compile-for-flymake" ,temp-file) -- 2.47.0