]> git.eshelyaron.com Git - emacs.git/commitdiff
Improve native--compile-skip-on-battery-p
authorSean Whitton <spwhitton@spwhitton.name>
Sat, 3 May 2025 02:32:42 +0000 (10:32 +0800)
committerEshel Yaron <me@eshelyaron.com>
Sat, 10 May 2025 06:57:00 +0000 (08:57 +0200)
* lisp/emacs-lisp/comp-run.el
(native--compile-skip-on-battery-p): Look at %L, %b and %B
format characters.  Add commentary.

(cherry picked from commit f180e4c9cb676e771acdaed7b0fd76c1c83dd485)

lisp/emacs-lisp/comp-run.el

index 89fd8cf68a95ca6731bcb87d2d425228513750ac..062047abce75cccb729bfc71e50bc413ed5264c9 100644 (file)
@@ -54,9 +54,9 @@ 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
+;; TODO 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
@@ -179,11 +179,28 @@ LOAD and SELECTOR work as described in `native--compile-async'."
 
 (defun native--compile-skip-on-battery-p ()
   "Should we skip JIT compilation because we're running on battery power?"
+  ;; The `battery-status-function' API is not specified so as to
+  ;; render it cleanly machine-readable, so we resort to heuristics.
+  ;; We could extend the API to return machine-readable information in
+  ;; the alist when an optional boolean argument is provided to the
+  ;; `battery-status-function'; we could use `func-arity' to check
+  ;; whether a custom `battery-status-function' supports the extension.
+  ;; However, so far in the time we've had battery.el, it would appear
+  ;; that this is the first time we've wanted to use the information
+  ;; other than just for generating messages.
   (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"))))))
+             (res (funcall battery-status-function))
+             ((or (member (cdr (assq ?L res)) '("off-line" "BAT" "Battery"))
+                  ;; If %L has not given us what we need, we don't
+                  ;; consider battery charge levels or percentages,
+                  ;; because power users often configure their batteries
+                  ;; to stop charging at less than 100% as a way to
+                  ;; extend the lifetime of their battery hardware.
+                  (string= (cdr (assq ?b res)) "+")
+                  (member (cdr (assq ?B res)) '("charging" "pending-charge"))
+                  (not (string= (cdr (assq ?B res)) "discharging")))))))
 
 (defvar comp-files-queue ()
   "List of Emacs Lisp files to be compiled.")