]> git.eshelyaron.com Git - emacs.git/commitdiff
New user option native-comp-async-on-battery-power
authorSean Whitton <spwhitton@spwhitton.name>
Fri, 2 May 2025 04:04:26 +0000 (12:04 +0800)
committerEshel Yaron <me@eshelyaron.com>
Sat, 10 May 2025 06:55:21 +0000 (08:55 +0200)
* lisp/emacs-lisp/comp-run.el
(native-comp-async-on-battery-power): New option.
(battery-status-function): Declare.
(native--compile-skip-on-battery-p): New function.
(comp--run-async-workers): Call it.
* etc/NEWS: Announce the new option.

(cherry picked from commit 6b11687555cdd1d6896bad5eb09af217b28d6076)

lisp/emacs-lisp/comp-run.el

index 3dbf24a8be789c894083e7a41cc76bb940ee9e7c..89fd8cf68a95ca6731bcb87d2d425228513750ac 100644 (file)
@@ -54,6 +54,23 @@ or one if there's just one execution unit."
   :risky t
   :version "28.1")
 
+;; If we could start compilations that were skipped if and when AC power
+;; is subsequently reconnected, we could consider changing the default
+;; to nil.  --spwhitton
+(defcustom native-comp-async-on-battery-power t
+  "Whether to start asynchronous native compilation while on battery power.
+Customize this to nil to disable starting async compilations when AC
+power is not connected.  Async compilations that are already running
+when AC power is disconnected are not affected.  Compilations skipped
+because AC power was disconnected are not started again if AC power is
+subsequently reconnected; in most cases, those compilations will not be
+tried again until after you next restart Emacs.
+
+Customizing this to nil has no effect unless `battery-status-function'
+can correctly detect the absence of connected AC power on your platform."
+  :type 'boolean
+  :version "31.1")
+
 (defcustom native-comp-async-report-warnings-errors t
   "Whether to report warnings and errors from asynchronous native compilation.
 
@@ -158,6 +175,16 @@ LOAD and SELECTOR work as described in `native--compile-async'."
                       (string-match-p re file))
                     native-comp-jit-compilation-deny-list))))
 
+(defvar battery-status-function)
+
+(defun native--compile-skip-on-battery-p ()
+  "Should we skip JIT compilation because we're running on battery power?"
+  (and-let* (((not native-comp-async-on-battery-power))
+             ((require 'battery))
+             battery-status-function
+             (status (assq ?L (funcall battery-status-function)))
+             ((not (string= (cdr status) "on-line"))))))
+
 (defvar comp-files-queue ()
   "List of Emacs Lisp files to be compiled.")
 
@@ -221,7 +248,8 @@ display a message."
   (cl-assert (null comp-no-spawn))
   (if (or comp-files-queue
           (> (comp--async-runnings) 0))
-      (unless (>= (comp--async-runnings) (comp--effective-async-max-jobs))
+      (unless (or (>= (comp--async-runnings) (comp--effective-async-max-jobs))
+                  (native--compile-skip-on-battery-p))
         (cl-loop
          for (source-file . load) = (pop comp-files-queue)
          while source-file