emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/compat 1bee325228 1/3: Introduce compat-guard helper ma


From: ELPA Syncer
Subject: [elpa] externals/compat 1bee325228 1/3: Introduce compat-guard helper macro
Date: Mon, 16 Jan 2023 20:57:27 -0500 (EST)

branch: externals/compat
commit 1bee3252289e21728b18cdf9f3dd76957380e57f
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>

    Introduce compat-guard helper macro
---
 compat-26.el   |  3 +--
 compat-27.el   | 17 +++++++----------
 compat-macs.el | 50 ++++++++++++++++++++++++++------------------------
 3 files changed, 34 insertions(+), 36 deletions(-)

diff --git a/compat-26.el b/compat-26.el
index 43770f3582..9833698c9c 100644
--- a/compat-26.el
+++ b/compat-26.el
@@ -88,8 +88,7 @@ SEQUENCE may be a list, a vector, a boolean vector, or a 
string."
         (if entry (cdr entry) default))
     (alist-get key alist default remove)))
 
-;; NOTE: Define gv expander only if `compat--alist-get' is defined.
-(when (eval-when-compile (< emacs-major-version 26))
+(compat-guard
   (gv-define-expander compat--alist-get ;; <compat-tests:alist-get-gv>
     (lambda (do key alist &optional default remove testfn)
       (macroexp-let2 macroexp-copyable-p k key
diff --git a/compat-27.el b/compat-27.el
index a7a1754fd1..fa94a2073b 100644
--- a/compat-27.el
+++ b/compat-27.el
@@ -283,7 +283,7 @@ return nil."
 
 ;;;; Defined in simple.el
 
-(when (eval-when-compile (< emacs-major-version 27))
+(compat-guard
   (cl-defstruct (decoded-time ;; <compat-tests:decoded-time>
                  (:constructor nil)
                  (:copier nil)
@@ -335,15 +335,14 @@ Internal use only."
     (setcdr image (plist-put (cdr image) property value)))
   value)
 
-(if (eval-when-compile (< emacs-major-version 26))
-    (with-eval-after-load 'image
-      (gv-define-simple-setter image-property image--set-property)) ;; 
<compat-tests:image-property>
+(compat-guard
+  :feature image
   ;; HACK: image--set-property was broken with an off-by-one error on Emacs 26.
   ;; The bug was fixed in a4ad7bed187493c1c230f223b52c71f5c34f7c89. Therefore 
we
   ;; override the gv expander until Emacs 27.1.
-  (when (eval-when-compile (< emacs-major-version 27))
-    (with-eval-after-load 'image
-      (gv-define-simple-setter image-property compat--image--set-property)))) 
;; <compat-tests:image-property>
+  (if (eval-when-compile (< emacs-major-version 26))
+      (gv-define-simple-setter image-property image--set-property) ;; 
<compat-tests:image-property>
+    (gv-define-simple-setter image-property compat--image--set-property)))
 
 ;;;; Defined in files.el
 
@@ -524,9 +523,7 @@ January 1st being 1."
 
 ;;;; Defined in text-property-search.el
 
-(declare-function make-prop-match nil)
-(when (eval-when-compile (< emacs-major-version 27))
-  (cl-defstruct (prop-match) beginning end value)) ;; <compat-tests:prop-match>
+(compat-guard (cl-defstruct (prop-match) beginning end value)) ;; 
<compat-tests:prop-match>
 
 (compat-defun text-property-search-forward ;; 
<compat-tests:text-property-search-forward>
     (property &optional value predicate not-current)
diff --git a/compat-macs.el b/compat-macs.el
index 62305efc00..54a10eca82 100644
--- a/compat-macs.el
+++ b/compat-macs.el
@@ -35,6 +35,21 @@
     (when (and (< 24 before) (< emacs-major-version before))
       `(require ',(intern (format "compat-%d" before))))))
 
+(defmacro compat-guard (&rest rest)
+  "Guard definition with a version check.
+REST is an attribute plist followed by the definition body.  The
+attributes specify the conditions under which the definition is
+generated.
+
+- :feature :: Wrap the definition with `with-eval-after-load'.
+
+- :when :: Do not install the definition depending on the
+  version.  Instead install the definition if :when evaluates to
+  non-nil."
+  (declare (debug ([&rest keywordp sexp] def-body))
+           (indent 0))
+  (compat--guard rest '(:body) #'identity))
+
 (defun compat--format-docstring (type name docstring)
   "Format DOCSTRING for NAME of TYPE.
 Prepend compatibility notice to the actual documentation string."
@@ -61,7 +76,7 @@ If this is not documented on yourself system, you can check \
     (setq attrs (cddr attrs)))
   attrs)
 
-(defun compat--guarded-definition (attrs args fun)
+(defun compat--guard (attrs args fun)
   "Guard compatibility definition generation.
 The version constraints specified by ATTRS are checked.
 ARGS is a list of keywords which are looked up and passed to FUN."
@@ -87,10 +102,10 @@ ARGS is a list of keywords which are looked up and passed 
to FUN."
             `(with-eval-after-load ',feature ,@body)
           (macroexp-progn body))))))
 
-(defun compat--function-definition (type name arglist docstring rest)
+(defun compat--guarded-function (type name arglist docstring rest)
   "Define function NAME of TYPE with ARGLIST and DOCSTRING.
 REST are attributes and the function BODY."
-  (compat--guarded-definition rest '(:explicit :body)
+  (compat--guard rest '(:explicit :body)
     (lambda (explicit body)
       ;; Remove unsupported declares.  It might be possible to set these
       ;; properties otherwise.  That should be looked into and implemented
@@ -128,13 +143,9 @@ under which the definition is generated.
 
 - :obsolete :: Mark the alias as obsolete.
 
-- :feature :: Wrap the definition with `with-eval-after-load'.
-
-- :when :: Do not install the definition depending on the
-  version.  Instead install the definition if :when evaluates to
-  non-nil."
+- :feature and :when :: See `compat-guard'."
   (declare (debug (name symbolp [&rest keywordp sexp])))
-  (compat--guarded-definition attrs '(:obsolete)
+  (compat--guard attrs '(:obsolete)
     (lambda (obsolete)
       ;; The fboundp check is performed at runtime to make sure that we never
       ;; redefine an existing definition if Compat is loaded on a newer Emacs
@@ -152,23 +163,18 @@ under which the definition is generated.
   "Define compatibility function NAME with arguments ARGLIST.
 The function must be documented in DOCSTRING.  REST is an
 attribute plist followed by the function body.  The attributes
-specify the conditions under which the compatiblity function is
-defined.
+specify the conditions under which the definition is generated.
 
 - :explicit :: Make the definition available such that it can be
   called explicitly via `compat-call'.
 
-- :feature :: Wrap the definition with `with-eval-after-load'.
-
-- :when :: Do not install the definition depending on the
-  version.  Instead install the definition if :when evaluates to
-  non-nil."
+- :feature and :when :: See `compat-guard'."
   (declare (debug (&define name (&rest symbolp)
                            stringp
                            [&rest keywordp sexp]
                            def-body))
            (doc-string 3) (indent 2))
-  (compat--function-definition 'function name arglist docstring rest))
+  (compat--guarded-function 'function name arglist docstring rest))
 
 (defmacro compat-defmacro (name arglist docstring &rest rest)
   "Define compatibility macro NAME with arguments ARGLIST.
@@ -176,7 +182,7 @@ The macro must be documented in DOCSTRING.  REST is an 
attribute
 plist followed by the macro body.  See `compat-defun' for
 details."
   (declare (debug compat-defun) (doc-string 3) (indent 2))
-  (compat--function-definition 'macro name arglist docstring rest))
+  (compat--guarded-function 'macro name arglist docstring rest))
 
 (defmacro compat-defvar (name initval docstring &rest attrs)
   "Define compatibility variable NAME with initial value INITVAL.
@@ -190,14 +196,10 @@ definition is generated.
   `permanent'.  For other non-nil values make the variable
   buffer-local.
 
-- :feature :: Wrap the definition with `with-eval-after-load'.
-
-- :when :: Do not install the definition depending on the
-  version.  Instead install the definition if :when evaluates to
-  non-nil."
+- :feature and :when :: See `compat-guard'."
   (declare (debug (name form stringp [&rest keywordp sexp]))
            (doc-string 3) (indent 2))
-  (compat--guarded-definition attrs '(:local :constant)
+  (compat--guard attrs '(:local :constant)
     (lambda (local constant)
       ;; The boundp check is performed at runtime to make sure that we never
       ;; redefine an existing definition if Compat is loaded on a newer Emacs



reply via email to

[Prev in Thread] Current Thread [Next in Thread]