Confirmed with the following, simpler, test case:

Yodel[1] Report 2021-10-05 22:07:33
===================================

--8<---------------cut here---------------start------------->8---
(yodel
 :user-dir "org-save-all-org-buffers"
 :packages* org
 :formatter yodel-format-as-mailing-list-message
 :post*
 (yodel-file "./test.org"
   :with*
   "#+startup: overview
* A
** B"
   :then*
   (require 'org-element)
   (defun +org-visible nil
     (org-element-interpret-data
      (org-element-parse-buffer nil 'visible-only)))
   (message "%s
%s" "Before `org-save-all-org-buffers':"
(+org-visible))
   (set-buffer-modified-p t)
   (org-save-all-org-buffers)
   (message "%s
%s" "After `org-save-all-org-buffers':"
(+org-visible))))
--8<---------------cut here---------------end--------------->8---

STDOUT
======

Loading /tmp/org-save-all-org-buffers/straight-bootstrap-snippet.el (source)...
Before `org-save-all-org-buffers':

#+startup: overview
* A
** B

Saving all Org buffers...

Saving all Org buffers... done
After `org-save-all-org-buffers':

#+startup: overview
* A

Environment
===========

- emacs version: GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo version 1.17.4, Xaw3d scroll bars)
of 2021-09-29
- system type: gnu/linux

Packages
========

- org https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=cc2490a7061955395c4f5a1a23a088044554a2f7

The behavior of `save-some-buffers' PRED argument changed recently:

https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=a9ad3d477441feefa3bf6107d58281cb64e0e78a

If the PRED returns a function, that function is called.
Since `derived-mode-p' returns the symbol `org-mode', Org is being reloaded in modified buffers. That's what is causing the visibility change. This could also have other undesirable behavior such as running the mode hook, resetting buffer-local variables, etc.

The attached patch ensures we're returning a boolean from the PRED function.
Tested with:

Yodel[1] Report 2021-10-05 22:07:33
===================================

--8<---------------cut here---------------start------------->8---
(yodel
 :user-dir "org-save-all-org-buffers.patch"
 :packages*
(org :host github :repo "progfolio/org-mode" :branch "fix/org-save-all-org-buffers")
 :formatter yodel-format-as-mailing-list-message
 :post*
 (yodel-file "./test.org"
   :with*
   "#+startup: overview
* A
** B"
   :then*
   (require 'org-element)
   (defun +org-visible nil
     (org-element-interpret-data
      (org-element-parse-buffer nil 'visible-only)))
   (message "%s
%s" "Before `org-save-all-org-buffers':"
(+org-visible))
   (set-buffer-modified-p t)
   (org-save-all-org-buffers)
   (message "%s
%s" "After `org-save-all-org-buffers':"
(+org-visible))))
--8<---------------cut here---------------end--------------->8---

STDOUT
======

Loading /tmp/org-save-all-org-buffers.patch/straight-bootstrap-snippet.el (source)...
Bootstrapping straight.el...
Bootstrapping straight.el...done
Rebuilding all packages due to build cache schema change
Looking for gnu-elpa-mirror recipe → Cloning melpa...
Looking for gnu-elpa-mirror recipe → Cloning melpa...done
Looking for emacsmirror-mirror recipe → Cloning gnu-elpa-mirror... Looking for emacsmirror-mirror recipe → Cloning gnu-elpa-mirror...done
Looking for emacsmirror-mirror recipe → Cloning el-get...
Looking for emacsmirror-mirror recipe → Cloning el-get...done
Looking for straight recipe → Cloning emacsmirror-mirror...
Looking for straight recipe → Cloning emacsmirror-mirror...done
Building straight...
Building straight...done
Cloning org...
Cloning org...done
Building org...
Building org...done

Before `org-save-all-org-buffers':

#+startup: overview
* A
** B

Saving all Org buffers...

Saving all Org buffers... done
After `org-save-all-org-buffers':

#+startup: overview
* A
** B

Environment
===========

- emacs version: GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo version 1.17.4, Xaw3d scroll bars)
of 2021-09-29
- system type: gnu/linux

Packages
========

- org https://github.com/progfolio/org-mode/commit/f1fc22f861ca9610ad4f1e1227660712b46337e4


[1] https://www.github.com/progfolio/yodel

>From f1fc22f861ca9610ad4f1e1227660712b46337e4 Mon Sep 17 00:00:00 2001
From: Nicholas Vollmer <iarchivedmywholel...@gmail.com>
Date: Tue, 5 Oct 2021 21:07:01 -0400
Subject: [PATCH] lisp/org.el: (org-save-all-org-buffers): Prevent `org-mode'
 reload

* lisp/org.el: (org-save-all-org-buffers): Ensure `save-some-buffers' PRED returns boolean.

As of this upstream commit:

https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=a9ad3d477441feefa3bf6107d58281cb64e0e78a

`save-some-buffers' will call its PRED argument if it returns a function.
Since (derived-mode-p 'org-mode) returns the symbol org-mode,
and org-mode is a function, org-mode is reloaded in modified Org
buffers when calling `org-save-all-org-buffers'. Among other
undesirable behavior, this will cause the buffer's visibility to be
reset to its initial visibility.
---
 lisp/org.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/org.el b/lisp/org.el
index 18cb53af6..6ddcc6af7 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -15366,7 +15366,7 @@ The value is a list, with zero or more of the symbols `effort', `appt',
   "Save all Org buffers without user confirmation."
   (interactive)
   (message "Saving all Org buffers...")
-  (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
+  (save-some-buffers t (lambda () (and (derived-mode-p 'org-mode) t)))
   (when (featurep 'org-id) (org-id-locations-save))
   (message "Saving all Org buffers... done"))
 
-- 
2.33.0

Reply via email to