Kyle Meyer <k...@kyleam.com> writes:

>> -        (org-replace-buffer-contents write-back-buf 0.1 nil)
>> +        (replace-region-contents beg end write-back-buf 0.1 nil)
>
> Is it valid to pass a buffer as the third argument of
> replace-region-contents on Emacs 30 or earlier?  If I understand
> correctly, that capability came with Emacs's 7c82cc8b975 (2025-03-29).

You are right. What about the attached alternative fix (applied after
reverting d2e1c6789)?

> For reference, the changes to org-edit-src-save on Emacs's side came in
> 6bcf41c311b (Org: Use new `replace-region-contents`, 2025-03-29).  I was
> not planning to pull in that commit's changes without adjustments [*],
> but it may be a good idea to follow that commit's general approach.

> [*] ... at the very least, the changes from Org's 2d9e70b80
>     (org-edit-src-*: When writing back, do not displace point in undo
>     list, 2024-07-09) should be considered.  Plus, for compatibility, I
>     think we'd want to keep org-replace-buffer-contents around but mark
>     it as obsolete.

Stefan's commit removes code repetition. As for 2d9e70b80, it does not
look like Stefan's commit changes anything compared to the existing
behavior where we do (goto-char (point-max)). Stefan even added an
assertion, making sure that point is at the right place. So, we should
be safe I think.

What about the following plan:

1. Revert d2e1c6789 on bugfix
2. Apply my patch on bugfix
3. Revert 452584d9db53 on main
4. Merge bugfix into main
5. Adapt Stefan's commit and apply it on main only (to be extra safe)
6. Re-apply a different version of 452584d9db53
   obsoleting org-replace-buffer-contents by org-replace-region-contents
   *only on main*

>From 3b0f08f8424b3fa2acc53a424e9ba2a97997fa1c Mon Sep 17 00:00:00 2001
Message-ID: <3b0f08f8424b3fa2acc53a424e9ba2a97997fa1c.1746169430.git.yanta...@posteo.net>
From: Ihor Radchenko <yanta...@posteo.net>
Date: Fri, 2 May 2025 09:01:56 +0200
Subject: [PATCH] org-replace-buffer-contents: Do not use obsolete
 `replace-buffer-contents' in Emacs 31+

* lisp/org-compat.el (org-replace-buffer-contents): In Emacs 31 and
newer, where `replace-buffer-contents' is obsolete, use
`replace-region-contents' with the newly introduced SOURCE argument
value type (buffer).
---
 lisp/org-compat.el | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/lisp/org-compat.el b/lisp/org-compat.el
index 011d012355..c907039bb3 100644
--- a/lisp/org-compat.el
+++ b/lisp/org-compat.el
@@ -292,10 +292,16 @@   (defun org--flatten-tree (tree)
       (if tree (push tree elems))
       (nreverse elems))))
 
-(if (version< emacs-version "27.1")
+(with-no-warnings ; `replace-buffer-contents' is obsolete in Emacs 31
+  (cond
+   ((version< emacs-version "27.1")
     (defsubst org-replace-buffer-contents (source &optional _max-secs _max-costs)
-      (replace-buffer-contents source))
-  (defalias 'org-replace-buffer-contents #'replace-buffer-contents))
+      (replace-buffer-contents source)))
+   ((version< emacs-version "31")
+    (defalias 'org-replace-buffer-contents #'replace-buffer-contents))
+   (t
+    (defsubst org-replace-buffer-contents (source &optional max-secs max-costs)
+      (replace-region-contents (point-min) (point-max) source max-secs max-costs)))))
 
 (unless (fboundp 'proper-list-p)
   ;; `proper-list-p' was added in Emacs 27.1.  The function below is
-- 
2.49.0

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

Reply via email to