branch: externals/hyperbole
commit c0f33ab9893f44f2e131ad80ee0a3ef297bec317
Author: Bob Weiner <r...@gnu.org>
Commit: Bob Weiner <r...@gnu.org>

    hsys-org-fix-version - fix to reload Org libs from the proper path
    
    Conditionally define `string-replace' for Emacs27 compatibility
---
 ChangeLog                    | 13 ++++++++
 hsys-org.el                  | 74 +++++++++++++++++++++++++++++++++++++++++++-
 test/hy-test-dependencies.el | 23 +++++++++++---
 3 files changed, 104 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 2458ead204..2182bb37e4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2024-01-17  Bob Weiner  <r...@gnu.org>
+
+* test/hy-test-dependencies.el (org-reloaded): Show Org src dir.
+                               (string-replace): Conditionally define
+    this from "compat" package, since is from Emacs28 and not available
+    in Emacs27.
+
+* hsys-org.el (hsys-org-fix-version): Rewrite to reload any Org libraries
+    previously loaded from the wrong builtin Org path, if a newer
+    user-installed Org version exists on the 'load-path'.
+              (hsys-org-get-libraries-to-reload): Add to compute wrong
+    Org libraries loaded and use in above function.
+
 2024-01-16  Mats Lidell  <ma...@gnu.org>
 
 * Makefile (eln): Use echo target for showing build info as part of the
diff --git a/hsys-org.el b/hsys-org.el
index 69c9c969db..f8428047cd 100644
--- a/hsys-org.el
+++ b/hsys-org.el
@@ -3,7 +3,7 @@
 ;; Author:       Bob Weiner
 ;;
 ;; Orig-Date:     2-Jul-16 at 14:54:14
-;; Last-Mod:     16-Jan-24 at 00:16:13 by Bob Weiner
+;; Last-Mod:     17-Jan-24 at 22:49:28 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -158,6 +158,78 @@ an error."
 (defun hsys-org-fix-version ()
   "If multiple Org versions are loaded, use the one first on `load-path'.
 Always ensure Org libraries have been required.
+Return t if Org is reloaded, else nil."
+  ;; Not all versions of org include these variables, so set them
+  (setq org--inhibit-version-check nil
+       org-list-allow-alphabetical nil)
+  (let ((org-dir (ignore-errors (org-find-library-dir "org")))
+       (org-install-dir
+        (ignore-errors (org-find-library-dir "org-loaddefs"))))
+    (cond ((and org-dir org-install-dir (string-equal org-dir org-install-dir)
+               ;; Still may have a situation where the Org version matches the
+               ;; builtin Org but the directories are for a newer Org
+               ;; package version.
+               (if (string-match "[\\/]org-\\([0-9.]+-?[a-z]*\\)" org-dir)
+                   (string-equal (match-string 1 org-dir) ;; org-dir version
+                                 (remove ?- (org-release)))
+                 t))
+          ;; Just require these libraries used for Hyperbole testing to ensure
+          ;; they are loaded from the single Org version used.
+          (mapc (lambda (lib-sym) (require lib-sym nil t))
+                '(org-version org-keys org-compat ol org-table org-macs org-id
+                              org-element org-list org-element org-src 
org-fold org))
+          nil)
+         (t
+          ;; Ensure using any local available packaged version of Org mode
+          ;; rather than built-in which may have been activated before
+          ;; load-path was set correctly.  Avoids mixed version load of Org.
+          (let ((org-libraries-to-reload (hsys-org-get-libraries-to-reload))
+                lib-sym)
+            ;; Unload org libraries loaded with wrong path
+            (mapc (lambda (lib)
+                    (setq lib-sym (intern-soft lib))
+                    (when (featurep lib-sym) (unload-feature lib-sym t)))
+                  org-libraries-to-reload)
+
+            ;; Ensure user's external Org package version is configured for 
loading
+            (package-initialize)
+            (let ((pkg-desc (car (cdr (assq 'org package-archive-contents)))))
+              (package-activate pkg-desc t))
+
+            ;; Load org libraries with right path but save "org" for last
+            (mapc #'load (remove "org" org-libraries-to-reload))
+            (load "org")
+            ;; Next setting may have been deleted with the library
+            ;; unloading, so restore it.
+            (add-to-list 'auto-mode-alist '("\\.org\\'" . org-mode))
+            t)))))
+
+(defun hsys-org-get-libraries-to-reload ()
+  (interactive)
+  (let* ((builtin-org-dir (expand-file-name "../lisp/org/" data-directory))
+        (default-directory builtin-org-dir)
+        (builtin-org-files (nconc (file-expand-wildcards "*.el.gz")
+                                  (file-expand-wildcards "*.el")))
+        (feature-sym)
+        (file-to-load)
+        (builtin-org-libraries-loaded
+         (delq nil (mapcar (lambda (f)
+                             (setq file-to-load
+                                   ;; Get rid of both .el and .el.gz suffixes
+                                   (file-name-sans-extension
+                                    (file-name-sans-extension f))
+                                   feature-sym (intern-soft file-to-load))
+                             (and (featurep feature-sym)
+                                  (string-prefix-p builtin-org-dir
+                                                   (symbol-file feature-sym))
+                                  file-to-load))
+                           builtin-org-files))))
+    builtin-org-libraries-loaded))
+
+;; !! Delete this after fully testing replacement version
+(defun hsys-org-OLD-fix-version ()
+  "If multiple Org versions are loaded, use the one first on `load-path'.
+Always ensure Org libraries have been required.
 Return t if Org is reloaded, else nil."
   ;; Not all versions of org include this variable, so set it
   (setq org--inhibit-version-check nil
diff --git a/test/hy-test-dependencies.el b/test/hy-test-dependencies.el
index 7fc56087c5..66d6507971 100644
--- a/test/hy-test-dependencies.el
+++ b/test/hy-test-dependencies.el
@@ -3,7 +3,7 @@
 ;; Author:       Mats Lidell <ma...@gnu.org>
 ;;
 ;; Orig-Date:    20-Feb-21 at 23:16:00
-;; Last-Mod:     13-Jan-24 at 20:08:39 by Bob Weiner
+;; Last-Mod:     17-Jan-24 at 23:32:33 by Bob Weiner
 ;;
 ;; SPDX-License-Identifier: GPL-3.0-or-later
 ;;
@@ -36,16 +36,29 @@
 ;; Needed when `hypb:display-file-with-logo' uses `org-mode'.
 (setq hsys-org-enable-smart-keys t)
 
+;; From compat.el package
+(unless (fboundp 'string-replace)
+(defun string-replace (fromstring tostring instring)
+  "Replace FROMSTRING with TOSTRING in INSTRING each time it occurs."
+  (when (equal fromstring "")
+    (signal 'wrong-length-argument '(0)))
+  (let ((case-fold-search nil))
+    (replace-regexp-in-string
+     (regexp-quote fromstring)
+     tostring instring
+     t t))))
+
 (require 'pp)
 (terpri)
-(print (format "org-directory = %S" (ignore-errors (org-find-library-dir 
"org"))))
-(print (format "ord-load-dir  = %S" (ignore-errors (org-find-library-dir 
"org-loaddefs"))))
-(print (format "version       = %S" (org-release)))
+(print (format "Org source dir = %S" (ignore-errors (org-find-library-dir 
"org"))))
+(print (format "Org load dir   = %S" (ignore-errors (org-find-library-dir 
"org-loaddefs"))))
+(print (format "Org version    = %S" (org-release)))
 (terpri)
 
 (let ((org-reloaded (hsys-org-fix-version)))
   (if org-reloaded
-      (message "Mixed Org versions fixed and reloaded; version is now %s" 
org-version)
+      (message "Mixed Org versions fixed and reloaded\n  version is now %s\n  
source dir is now %S"
+              org-version (ignore-errors (org-find-library-dir "org")))
     (message "Correct, single version of Org is active %s" org-version)))
 
 (provide 'hy-test-dependencies)

Reply via email to