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

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

[elpa] externals/compat 9e40cf3c3a 01/10: Simplify loading and compiling


From: ELPA Syncer
Subject: [elpa] externals/compat 9e40cf3c3a 01/10: Simplify loading and compiling of Compat
Date: Sun, 17 Jul 2022 17:57:24 -0400 (EDT)

branch: externals/compat
commit 9e40cf3c3a4d36c39fb9145776e21d2f6be69de6
Author: Philip Kaludercic <philipk@posteo.net>
Commit: Philip Kaludercic <philipk@posteo.net>

    Simplify loading and compiling of Compat
    
    To avoid the issue like having to find the source of the other files
    in the project during byte compilation, we will stop embedding (or
    "entwining") the compiled results of compat-*.el into compat.elc.
    While this may make loading slightly slower -- in my own tests it was
    unnoticeable -- it makes loading Compat a lot easier to manage and
    decreases the risk of running into peculiar edge cases, as documented
    here[0].
    
    To accommodate for Compat's unusual approach, we still need to do
    something unusual, namely rebind the `features' variable, that is used
    by `require' to check if a feature is already bound or not.  This is
    done so that while loading compat-NM.el, the bound version of
    `features' is updated but the updated version is reverted back as soon
    as the scope of the let-block is left.  This allows `compat-NM' to be
    loaded again later on, without `compat--inhibit-prefixed' being bound,
    as is the case in compat.el.  This variable, if bound, suppresses the
    evaluation of prefixed definitions, as had previously been done by the
    generator function `compat--generate-minimal-no-prefix'.
    
    Another marginal advantage of this approach is that if someone loads
    `compat-NM' before `compat' (for whatever reason), `compat-NM' will
    not be reloaded, as it is represented in both the actual as well as
    the copied value of `features'.
    
    [0] https://todo.sr.ht/~pkal/compat/4#event-180270
---
 compat-macs.el | 31 +++++++++++++----------
 compat.el      | 78 +++++++++++-----------------------------------------------
 2 files changed, 33 insertions(+), 76 deletions(-)

diff --git a/compat-macs.el b/compat-macs.el
index 85f31b8804..a2de41da7b 100644
--- a/compat-macs.el
+++ b/compat-macs.el
@@ -29,6 +29,17 @@
   "Ignore all arguments."
   nil)
 
+(defvar compat--inhibit-prefixed nil
+  "Non-nil means that prefixed definitions are not loaded.
+A prefixed function is something like `compat-assoc', that is
+only made visible when the respective compatibility version file
+is loaded (in this case `compat-26').")
+
+(defmacro compat--inhibit-prefixed (&rest body)
+  "Ignore BODY unless `compat--inhibit-prefixed' is true."
+  `(unless (bound-and-true-p compat--inhibit-prefixed)
+     ,@body))
+
 (defvar compat--generate-function #'compat--generate-minimal
   "Function used to generate compatibility code.
 The function must take six arguments: NAME, DEF-FN, INSTALL-FN,
@@ -84,7 +95,6 @@ DEF-FN, INSTALL-FN, CHECK-FN, ATTR and TYPE."
          (cond (plist-get attr :cond))
          (version ; If you edit this, also edit `compat--generate-verbose'.
           (or (plist-get attr :version)
-              (bound-and-true-p compat--entwine-version)
               (let* ((file (car (last current-load-list)))
                      (file (if (stringp file)
                                ;; Some library, which requires compat-XY.el,
@@ -92,7 +102,9 @@ DEF-FN, INSTALL-FN, CHECK-FN, ATTR and TYPE."
                                ;; been compiled yet.
                                file
                              ;; compat-XY.el is being compiled.
-                             (bound-and-true-p byte-compile-current-file))))
+                             (or (bound-and-true-p byte-compile-current-file)
+                                 ;; Fallback to the buffer being evaluated.
+                                 (buffer-file-name)))))
                 (if (and file
                          (string-match
                           "compat-\\([[:digit:]]+\\)\\.\\(?:elc?\\)\\'" file))
@@ -107,7 +119,7 @@ DEF-FN, INSTALL-FN, CHECK-FN, ATTR and TYPE."
                            (version< max-version emacs-version)))
                   '(compat--ignore))
                  ((plist-get attr :prefix)
-                  '(progn))
+                  '(compat--inhibit-prefixed))
                  ((and version (version<= version emacs-version) (not cond))
                   '(compat--ignore))
                  (`(when (and ,(if cond cond t)
@@ -143,13 +155,6 @@ DEF-FN, INSTALL-FN, CHECK-FN, ATTR and TYPE."
             `(eval-after-load ,feature `(funcall ',(lambda () ,body)))
           body))))))
 
-(defun compat--generate-minimal-no-prefix (name def-fn install-fn check-fn 
attr type)
-  "Generate a leaner compatibility definition.
-See `compat-generate-function' for details on the arguments NAME,
-DEF-FN, INSTALL-FN, CHECK-FN, ATTR and TYPE."
-  (unless (plist-get attr :prefix)
-    (compat--generate-minimal name def-fn install-fn check-fn attr type)))
-
 (defun compat--generate-verbose (name def-fn install-fn check-fn attr type)
   "Generate a more verbose compatibility definition, fit for testing.
 See `compat-generate-function' for details on the arguments NAME,
@@ -160,11 +165,11 @@ DEF-FN, INSTALL-FN, CHECK-FN, ATTR and TYPE."
          (cond (plist-get attr :cond))
          (version ; If you edit this, also edit `compat--generate-minimal'.
           (or (plist-get attr :version)
-              (bound-and-true-p compat--entwine-version)
               (let* ((file (car (last current-load-list)))
                      (file (if (stringp file)
                                file
-                             (bound-and-true-p byte-compile-current-file))))
+                             (or (bound-and-true-p byte-compile-current-file)
+                                 (buffer-file-name)))))
                 (if (and file
                          (string-match
                           "compat-\\([[:digit:]]+\\)\\.\\(?:elc?\\)\\'" file))
@@ -193,7 +198,7 @@ DEF-FN, INSTALL-FN, CHECK-FN, ATTR and TYPE."
                      (version< max-version emacs-version)))
             '(compat--ignore))
            ((plist-get attr :prefix)
-            '(progn))
+            '(compat--inhibit-prefixed))
            ((and version (version<= version emacs-version) (not cond))
             '(compat--ignore))
            (`(when (and ,(if cond cond t)
diff --git a/compat.el b/compat.el
index 9484211879..b8e5e93898 100644
--- a/compat.el
+++ b/compat.el
@@ -41,69 +41,21 @@
 
 (eval-when-compile (require 'compat-macs))
 
-;;;; Core functionality
-
-;; To accelerate the loading process, we insert the contents of
-;; compat-N.M.el directly into the compat.elc.  Note that by default
-;; this will not include prefix functions.  These have to be required
-;; separately, by explicitly requiring the feature that defines them.
-(eval-when-compile
-  (defvar compat--generate-function)
-  (defvar compat--entwine-version)
-  (defmacro compat-entwine (version)
-    (cond
-     ((or (not (eq compat--generate-function 'compat--generate-minimal))
-          (bound-and-true-p compat-testing))
-      `(load ,(format "compat-%d.el" version)))
-     ((let* ((compat--generate-function 'compat--generate-minimal-no-prefix)
-             (file (expand-file-name
-                    (format "compat-%d.el" version)
-                    (file-name-directory
-                     (or
-                      ;; Some third-party library, which requires
-                      ;; compat.el, is being compiled, loaded or
-                      ;; evaluated, and compat.el hasn't been compiled
-                      ;; yet.
-                      ;;   cd compat && make clean && cd ../other && \
-                      ;;   make clean all
-                      ;;
-                      ;; Or compat.el is being evaluated.
-                      ;;   cd compat && make clean && emacs -Q -L . compat.el
-                      ;;   M-x eval-buffer
-                      ;;
-                      ;; (Like `macroexp-file-name' from Emacs 28.1.)
-                      (let ((file (car (last current-load-list))))
-                        (and (stringp file) file))
-                      ;; compat.el is being compiled.
-                      ;;   cd compat && make clean all
-                      (bound-and-true-p byte-compile-current-file)))))
-             (compat--entwine-version (number-to-string version))
-             defs)
-        (with-temp-buffer
-          (insert-file-contents file)
-          (emacs-lisp-mode)
-          (while (progn
-                   (forward-comment 1)
-                   (not (eobp)))
-            (let ((form (read (current-buffer))))
-              (cond
-               ((memq (car-safe form)
-                      '(compat-defun
-                           compat-defmacro
-                           compat-advise
-                         compat-defvar))
-                (push (macroexpand-all form) defs))
-               ((memq (car-safe form)
-                      '(declare-function
-                        defvar))
-                (push form defs))))))
-        (macroexp-progn (nreverse defs)))))))
-
-(compat-entwine 24)
-(compat-entwine 25)
-(compat-entwine 26)
-(compat-entwine 27)
-(compat-entwine 28)
+;; We load all the components of Compat with a copied value of
+;; `features' list, that will prevent the list being modified, and all
+;; the files can be loaded again.  This is done so that
+;; `compat--inhibit-prefixed' can take effect when loading `compat',
+;; and do nothing when loading each sub-feature manually.
+
+(defvar compat--inhibit-prefixed)
+(let* ((features (copy-sequence features))
+       (compat--inhibit-prefixed t))
+  (ignore features)                     ;for the byte compiler
+  (load "compat-24")
+  (load "compat-25")
+  (load "compat-26")
+  (load "compat-27")
+  (load "compat-28"))
 
 (provide 'compat)
 ;;; compat.el ends here



reply via email to

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