From 5bd2c405cfbf0b7adff3dbd2316baec650271eea Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Sat, 11 Feb 2023 01:46:07 +0100 Subject: More robust method to require libraries conditionally --- compat-25.el | 5 +++-- compat-26.el | 6 ++++-- compat-27.el | 8 +++++--- compat-28.el | 6 ++++-- compat-29.el | 12 +++++++----- compat-macs.el | 15 +++++++++------ compat.el | 10 ++++++++-- 7 files changed, 40 insertions(+), 22 deletions(-) diff --git a/compat-25.el b/compat-25.el index e710f86..f6ad2ad 100644 --- a/compat-25.el +++ b/compat-25.el @@ -21,8 +21,9 @@ ;;; Code: -(eval-when-compile (load "compat-macs.el" nil t t)) -(compat-declare-version "25.1") +(eval-when-compile (require 'compat-macs)) + +(compat-version "25.1") ;;;; Defined in alloc.c diff --git a/compat-26.el b/compat-26.el index 367b45e..956c4a7 100644 --- a/compat-26.el +++ b/compat-26.el @@ -21,8 +21,10 @@ ;;; Code: -(eval-when-compile (load "compat-macs.el" nil t t)) -(compat-declare-version "26.1") +(eval-when-compile (require 'compat-macs)) +(compat-require compat-25 "25.1") + +(compat-version "26.1") ;;;; Defined in fns.c diff --git a/compat-27.el b/compat-27.el index 19db3a6..30aa97c 100644 --- a/compat-27.el +++ b/compat-27.el @@ -21,8 +21,10 @@ ;;; Code: -(eval-when-compile (load "compat-macs.el" nil t t)) -(compat-declare-version "27.1") +(eval-when-compile (require 'compat-macs)) +(compat-require compat-26 "26.1") + +(compat-version "27.1") ;;;; Defined in fns.c @@ -825,7 +827,7 @@ discarded." ;;;; Defined in map-ynp.el -(compat-declare-version "26.2") +(compat-version "26.2") (compat-defvar read-answer-short 'auto ;; "If non-nil, the `read-answer' function accepts single-character answers. diff --git a/compat-28.el b/compat-28.el index e3bde86..57a40f7 100644 --- a/compat-28.el +++ b/compat-28.el @@ -21,8 +21,10 @@ ;;; Code: -(eval-when-compile (load "compat-macs.el" nil t t)) -(compat-declare-version "28.1") +(eval-when-compile (require 'compat-macs)) +(compat-require compat-27 "27.1") + +(compat-version "28.1") ;;;; Defined in fns.c diff --git a/compat-29.el b/compat-29.el index 11cccb6..9cf4a76 100644 --- a/compat-29.el +++ b/compat-29.el @@ -21,13 +21,15 @@ ;;; Code: -(eval-when-compile (load "compat-macs.el" nil t t)) -;; TODO Update to 29.1 as soon as the Emacs emacs-29 branch version bumped -(compat-declare-version "29.0") +(eval-when-compile (require 'compat-macs)) +(compat-require compat-28 "28.1") -;;;; Preloaded in loadup.el +;; Preloaded in loadup.el +;; TODO Update to 29.1 as soon as the Emacs emacs-29 branch version bumped +(compat-require seq "29.0") ;; -(compat-guard t (require 'seq)) ;; +;; TODO Update to 29.1 as soon as the Emacs emacs-29 branch version bumped +(compat-version "29.0") ;;;; Defined in xdisp.c diff --git a/compat-macs.el b/compat-macs.el index 25b1088..9370b02 100644 --- a/compat-macs.el +++ b/compat-macs.el @@ -250,13 +250,16 @@ definition is generated. ,@(and local `((make-variable-buffer-local ',name))) ,@(and (eq local 'permanent) `((put ',name 'permanent-local t))))))) -(defmacro compat-declare-version (version) +(defmacro compat-version (version) "Set the Emacs version that is currently being handled to VERSION." - (prog1 - (let ((before (1- (car (version-to-list version))))) - (when (and (< 24 before) (< emacs-major-version before)) - `(require ',(require (intern (format "compat-%d" before)))))) - (setq compat-macs--version version))) + (setq compat-macs--version version) + nil) + +(defmacro compat-require (feature version) + "Require FEATURE if the Emacs version is less than VERSION." + (when (version< emacs-version version) + (require feature) + `(require ',feature))) (provide 'compat-macs) ;;; compat-macs.el ends here diff --git a/compat.el b/compat.el index 56bed48..ed34965 100644 --- a/compat.el +++ b/compat.el @@ -46,8 +46,14 @@ ;;; Code: -(when (eval-when-compile (< emacs-major-version 29)) - (require 'compat-29)) +;; Ensure that the newest compatibility layer is required at compile +;; time and runtime, but only if needed. +(eval-when-compile + (defmacro compat--maybe-require-29 () + (when (< emacs-major-version 29) + (require 'compat-29) + '(require 'compat-29)))) +(compat--maybe-require-29) ;;;; Macros for extended compatibility function calls -- cgit v1.0