From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <emacs-orgmode-bounces+larch=yhetil.org@gnu.org>
Received: from mp12.migadu.com ([2001:41d0:2:c151::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by ms5.migadu.com with LMTPS
	id YPj7N/p9zWLjPQAAbAwnHQ
	(envelope-from <emacs-orgmode-bounces+larch=yhetil.org@gnu.org>)
	for <larch@yhetil.org>; Tue, 12 Jul 2022 15:58:18 +0200
Received: from aspmx2.migadu.com ([2001:41d0:2:c151::])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	by mp12.migadu.com with LMTPS
	id 0PjqN/p9zWI60AAAauVa8A
	(envelope-from <emacs-orgmode-bounces+larch=yhetil.org@gnu.org>)
	for <larch@yhetil.org>; Tue, 12 Jul 2022 15:58:18 +0200
Received: from lists.gnu.org (lists.gnu.org [209.51.188.17])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by aspmx2.migadu.com (Postfix) with ESMTPS id 3D10637548
	for <larch@yhetil.org>; Tue, 12 Jul 2022 15:58:14 +0200 (CEST)
Received: from localhost ([::1]:43792 helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <emacs-orgmode-bounces+larch=yhetil.org@gnu.org>)
	id 1oBGOz-00081S-Td
	for larch@yhetil.org; Tue, 12 Jul 2022 09:58:13 -0400
Received: from eggs.gnu.org ([2001:470:142:3::10]:45746)
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <yantar92@gmail.com>)
 id 1oBGNq-0007s9-DC
 for emacs-orgmode@gnu.org; Tue, 12 Jul 2022 09:57:05 -0400
Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]:46624)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <yantar92@gmail.com>)
 id 1oBGNm-0005Zf-Gu
 for emacs-orgmode@gnu.org; Tue, 12 Jul 2022 09:57:00 -0400
Received: by mail-pf1-x435.google.com with SMTP id a15so7514930pfv.13
 for <emacs-orgmode@gnu.org>; Tue, 12 Jul 2022 06:56:57 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
 h=from:to:cc:subject:in-reply-to:references:date:message-id
 :mime-version; bh=I8jhf0js7Rrqa2S6UsaQDPHcJ6fuETsGS6bJyyBIZNU=;
 b=A7R226WyTFSBKOqe1TPaLJtzq7P/yy3ejBjID0guDVDsWG4ig2tLwtOOh2s/BNfuZb
 T7MrffYdwL6eIovZrXiNtx303SdG3ApYoD9leC1v2hWwLfx1k0VHV4SC44sxcF36OH42
 XT8xhB/KVh5umwGwwgjkt+CJERBbpvPnm1xnnjbLSmxpAEbyfwrB4XAhce3+hmoD9GXn
 5W+y1dcyGXfr/mkWJHr6PjnARMuiQyp9qAZRLbFuYM2ObO4ZIsseYDWOyp4iaOw8lq/o
 DuYSp4rJtk/YjvifBbT5x+dftB1/jt3rIMGw6kj4QWd9i9QVcg47ZJKW596WCJRx46SM
 vHkw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20210112;
 h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date
 :message-id:mime-version;
 bh=I8jhf0js7Rrqa2S6UsaQDPHcJ6fuETsGS6bJyyBIZNU=;
 b=BkC+mRcjF0Ws8oRWsq385f0u6OAYC9xdf9fv/NuC3YerUfRR5Box67P/YMfFqL9XHr
 pUuob1e7A9kxZFVrNoQFbxukKhcIbD65Egg059K6q3SWU1BI3h2KsIW+I+MxzDSfBZCM
 YlVOVK1VKjZRgGj9xAmJRdxfg4YRQu3My7t6qtIK1cOPmNd4nY69bYXNryD5dkRyr1wW
 buXDdfSyhAKnFFRHWDI8aZ8XdmY8cZsO7GfIuJLU9T4WqWOUA9mSbmktdieKh01ZBwIb
 lIzjjUogk9FG8SNkvJnidPkIA5zjqtAIhR1cDaWapPeOxJkVBUnTldY2uejgPVGzqdyT
 yhkg==
X-Gm-Message-State: AJIora9IX+5w+Djtdy0xHXgUinfJUCjRObgIJ9TZT5kTXqdDUpXvrRfF
 y9DvpNComeNpbu8wf1FonFM=
X-Google-Smtp-Source: AGRyM1v9gKV5dTIZC3dZgFimDuAMNE3HKXhxUrMYTftXf/P73Ff2ljYSw2XhKrjZqR8OW2s36/ilIQ==
X-Received: by 2002:a05:6a00:852:b0:528:c669:ad65 with SMTP id
 q18-20020a056a00085200b00528c669ad65mr23710919pfk.75.1657634216713; 
 Tue, 12 Jul 2022 06:56:56 -0700 (PDT)
Received: from localhost ([107.179.24.4]) by smtp.gmail.com with ESMTPSA id
 w15-20020aa7954f000000b00528d620eb58sm7078404pfq.17.2022.07.12.06.56.54
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Tue, 12 Jul 2022 06:56:55 -0700 (PDT)
From: Ihor Radchenko <yantar92@gmail.com>
To: Jack Kamm <jackkamm@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: Recent folding issues
In-Reply-To: <871quqvorh.fsf@gmail.com>
References: <877d4jve13.fsf@gmail.com> <87ilo36su8.fsf@localhost>
 <871quqvorh.fsf@gmail.com>
Date: Tue, 12 Jul 2022 21:58:00 +0800
Message-ID: <87sfn6h1qv.fsf@localhost>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
Received-SPF: pass client-ip=2607:f8b0:4864:20::435;
 envelope-from=yantar92@gmail.com; helo=mail-pf1-x435.google.com
X-Spam_score_int: -18
X-Spam_score: -1.9
X-Spam_bar: -
X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
 FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001,
 RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,
 T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-BeenThere: emacs-orgmode@gnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: "General discussions about Org-mode." <emacs-orgmode.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/emacs-orgmode>,
 <mailto:emacs-orgmode-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/emacs-orgmode>
List-Post: <mailto:emacs-orgmode@gnu.org>
List-Help: <mailto:emacs-orgmode-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/emacs-orgmode>,
 <mailto:emacs-orgmode-request@gnu.org?subject=subscribe>
Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org
Sender: "Emacs-orgmode" <emacs-orgmode-bounces+larch=yhetil.org@gnu.org>
X-Migadu-Flow: FLOW_IN
X-Migadu-To: larch@yhetil.org
X-Migadu-Country: US
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org;
	s=key1; t=1657634298;
	h=from:from:sender:sender:reply-to:subject:subject:date:date:
	 message-id:message-id:to:to:cc:cc:mime-version:mime-version:
	 content-type:content-type:in-reply-to:in-reply-to:
	 references:references:list-id:list-help:list-unsubscribe:
	 list-subscribe:list-post:dkim-signature;
	bh=I8jhf0js7Rrqa2S6UsaQDPHcJ6fuETsGS6bJyyBIZNU=;
	b=I51bCaKBchrClpiawtrW25+iEra+us5gdvrKM9HKK+Bk3+8wyz6/pqKJjlHYdYt8Y/s5Rm
	K9OlFCT30Mc3yiIfDGWlbrrbuV2Rye0AogPItoxbIAfKn5TqJi5AP21/PVM8+3eRlX0qOy
	PeY1TDPGr4i2VdEGXjeG+UtI4P/LbXU2WzPxK8jduZVrEtM030GBbZaQwnNlL6vckUbaXW
	mY4kJDktZIMB0UkAwY3Qk04SORpRsEnrB4sWYmCXG77eBz2nCkjN/xudrVULwW+qRFf4QL
	+WVEJeRc+K739hNwnHqv3grWHzckLe/ZnjACtHKSyZNN+bxEvyIMOkG/PNDs7Q==
ARC-Seal: i=1; s=key1; d=yhetil.org; t=1657634298; a=rsa-sha256; cv=none;
	b=tNZNx4wwdTLt6sBh0tHxQgSkyd/8W7sbLMkQCGhZUufeAXhm6Z2K+XRhYYuv2djxQCZo2z
	/uqV3U9g7dII/9JegKF+NLLEjlpuL01Xc2yeHLSt1KervhMBG0chClnRh6V6moWWqQ/1p6
	BF35pfppC1VmC1JxNlS2H0ko7Iy79UXdCKX/xagcHun+N45eC7/TcvQuJHTdJaTN6sLlrL
	L8yUlYvsD9IoURofcTkKqBLjyIqWkg4jCeWTgZ4Bxn61aObLKVpjYBzUB3UcIC8IZBkRIP
	yngY4RuUxFFQRrghi3MtIU+xRaER5gwN6EppujUppkSQ1zLN6/CgW4I4xq92dw==
ARC-Authentication-Results: i=1;
	aspmx2.migadu.com;
	dkim=pass header.d=gmail.com header.s=20210112 header.b=A7R226Wy;
	dmarc=pass (policy=none) header.from=gmail.com;
	spf=pass (aspmx2.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"
X-Migadu-Spam-Score: -5.75
Authentication-Results: aspmx2.migadu.com;
	dkim=pass header.d=gmail.com header.s=20210112 header.b=A7R226Wy;
	dmarc=pass (policy=none) header.from=gmail.com;
	spf=pass (aspmx2.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"
X-Migadu-Queue-Id: 3D10637548
X-Spam-Score: -5.75
X-Migadu-Scanner: scn0.migadu.com
X-TUID: WKQ7bsMsDPck

--=-=-=
Content-Type: text/plain

Jack Kamm <jackkamm@gmail.com> writes:

>> I cannot reproduce. Please, update your Org to the latest version, try
>> to reproduce, and provide the detailed steps required to obtain the
>> confusing behaviour you are seeing.
>
> I attach a couple files for a minimal reproducible example of both
> issues.
>
> Steps to reproduce:

Thanks!
Can you try the attached patch set?

Best,
Ihor


--=-=-=
Content-Type: text/x-patch
Content-Disposition: inline;
 filename=0001-Revert-org.el-Improve-performance-of-deletion-comman.patch

>From a79a742cbff01b7815cffba806c28ea45a4da63c Mon Sep 17 00:00:00 2001
Message-Id: <a79a742cbff01b7815cffba806c28ea45a4da63c.1657634233.git.yantar92@gmail.com>
From: Ihor Radchenko <yantar92@gmail.com>
Date: Tue, 12 Jul 2022 15:05:45 +0800
Subject: [PATCH 1/3] Revert "org.el: Improve performance of deletion commands"

This reverts commit 46df681336c83c826b367d2803f59560165bdeba.

The optimization broke folding fragility checks.
---
 lisp/org.el | 74 ++++++++++++++++++++++++++---------------------------
 1 file changed, 36 insertions(+), 38 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 3d4de5b4f..d85b5818e 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16156,19 +16156,18 @@ (defun org-delete-backward-char (N)
 still be marked for re-alignment if the field did fill the entire column,
 because, in this case the deletion might narrow the column."
   (interactive "p")
-  (org-fold-core-ignore-modifications
-    (save-match-data
-      (org-fold-check-before-invisible-edit 'delete-backward)
-      (if (and (= N 1)
-	       (not overwrite-mode)
-	       (not (org-region-active-p))
-	       (not (eq (char-before) ?|))
-	       (save-excursion (skip-chars-backward " \t") (not (bolp)))
-	       (looking-at-p ".*?|")
-	       (org-at-table-p))
-	  (progn (forward-char -1) (org-delete-char 1))
-        (backward-delete-char N)
-        (org-fix-tags-on-the-fly)))))
+  (save-match-data
+    (org-fold-check-before-invisible-edit 'delete-backward)
+    (if (and (= N 1)
+	     (not overwrite-mode)
+	     (not (org-region-active-p))
+	     (not (eq (char-before) ?|))
+	     (save-excursion (skip-chars-backward " \t") (not (bolp)))
+	     (looking-at-p ".*?|")
+	     (org-at-table-p))
+	(progn (forward-char -1) (org-delete-char 1))
+      (backward-delete-char N)
+      (org-fix-tags-on-the-fly))))
 
 (defun org-delete-char (N)
   "Like `delete-char', but insert whitespace at field end in tables.
@@ -16177,31 +16176,30 @@ (defun org-delete-char (N)
 still be marked for re-alignment if the field did fill the entire column,
 because, in this case the deletion might narrow the column."
   (interactive "p")
-  (org-fold-core-ignore-modifications
-    (save-match-data
-      (org-fold-check-before-invisible-edit 'delete)
-      (cond
-       ((or (/= N 1)
-	    (eq (char-after) ?|)
-	    (save-excursion (skip-chars-backward " \t") (bolp))
-	    (not (org-at-table-p)))
-        (delete-char N)
-        (org-fix-tags-on-the-fly))
-       ((looking-at ".\\(.*?\\)|")
-        (let* ((update? org-table-may-need-update)
-	       (noalign (looking-at-p ".*?  |")))
-	  (delete-char 1)
-	  (org-table-with-shrunk-field
-	   (save-excursion
-	     ;; Last space is `org-table-separator-space', so insert
-	     ;; a regular one before it instead.
-	     (goto-char (- (match-end 0) 2))
-	     (insert " ")))
-	  ;; If there were two spaces at the end, this field does not
-	  ;; determine the width of the column.
-	  (when noalign (setq org-table-may-need-update update?))))
-       (t
-        (delete-char N))))))
+  (save-match-data
+    (org-fold-check-before-invisible-edit 'delete)
+    (cond
+     ((or (/= N 1)
+	  (eq (char-after) ?|)
+	  (save-excursion (skip-chars-backward " \t") (bolp))
+	  (not (org-at-table-p)))
+      (delete-char N)
+      (org-fix-tags-on-the-fly))
+     ((looking-at ".\\(.*?\\)|")
+      (let* ((update? org-table-may-need-update)
+	     (noalign (looking-at-p ".*?  |")))
+	(delete-char 1)
+	(org-table-with-shrunk-field
+	 (save-excursion
+	   ;; Last space is `org-table-separator-space', so insert
+	   ;; a regular one before it instead.
+	   (goto-char (- (match-end 0) 2))
+	   (insert " ")))
+	;; If there were two spaces at the end, this field does not
+	;; determine the width of the column.
+	(when noalign (setq org-table-may-need-update update?))))
+     (t
+      (delete-char N)))))
 
 ;; Make `delete-selection-mode' work with Org mode and Orgtbl mode
 (put 'org-self-insert-command 'delete-selection
-- 
2.35.1


--=-=-=
Content-Type: text/x-patch
Content-Disposition: inline;
 filename=0002-test-org-fold-Cover-the-issue-fixed-by-previous-comm.patch

>From f29ccf3161a51f700bb3375a16acab563baceb49 Mon Sep 17 00:00:00 2001
Message-Id: <f29ccf3161a51f700bb3375a16acab563baceb49.1657634233.git.yantar92@gmail.com>
In-Reply-To: <a79a742cbff01b7815cffba806c28ea45a4da63c.1657634233.git.yantar92@gmail.com>
References: <a79a742cbff01b7815cffba806c28ea45a4da63c.1657634233.git.yantar92@gmail.com>
From: Ihor Radchenko <yantar92@gmail.com>
Date: Tue, 12 Jul 2022 21:30:58 +0800
Subject: [PATCH 2/3] test-org-fold: Cover the issue fixed by previous commit

*
testing/lisp/test-org-fold.el (test-org-fold/org-fold-reveal-broken-structure):
Test `org-delete-char' instead of `delete-char'.  The former is the
function used interactively.
---
 testing/lisp/test-org-fold.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/testing/lisp/test-org-fold.el b/testing/lisp/test-org-fold.el
index 40afe55ae..a26346175 100644
--- a/testing/lisp/test-org-fold.el
+++ b/testing/lisp/test-org-fold.el
@@ -410,7 +410,7 @@ (ert-deftest test-org-fold/org-fold-reveal-broken-structure ()
       (re-search-forward "Text")
       (should (org-invisible-p))
       (goto-char 1)
-      (delete-char 1)
+      (org-delete-char 1)
       (re-search-forward "Text")
       (should-not (org-invisible-p)))
     (org-test-with-temp-text
-- 
2.35.1


--=-=-=
Content-Type: text/x-patch
Content-Disposition: inline;
 filename=0003-org-fold-Make-sure-that-changes-do-not-leave-partial.patch

>From 8e2fed82ed9335680f6b5a7d02028786d8479ca3 Mon Sep 17 00:00:00 2001
Message-Id: <8e2fed82ed9335680f6b5a7d02028786d8479ca3.1657634233.git.yantar92@gmail.com>
In-Reply-To: <a79a742cbff01b7815cffba806c28ea45a4da63c.1657634233.git.yantar92@gmail.com>
References: <a79a742cbff01b7815cffba806c28ea45a4da63c.1657634233.git.yantar92@gmail.com>
From: Ihor Radchenko <yantar92@gmail.com>
Date: Tue, 12 Jul 2022 21:32:08 +0800
Subject: [PATCH 3/3] org-fold: Make sure that changes do not leave partially
 folded headings

* lisp/org-fold-core.el (org-fold-core--fix-folded-region): Check
fragility in all the indirect buffers.
* lisp/org-fold.el (org-fold--reveal-headline-at-point): New function
revealing heading line or full heading when the heading only contains
blank lines.
(org-fold--reveal-outline-maybe): Check headline at the end of folded
region as well.  Use `org-fold-core--fix-folded-region'.
* lisp/org.el (org-insert-heading): Make sure that the newline in
front of heading is revealed.
---
 lisp/org-fold-core.el | 71 ++++++++++++++++++++++---------------------
 lisp/org-fold.el      | 52 ++++++++++++++++++++++---------
 lisp/org.el           |  5 ++-
 3 files changed, 77 insertions(+), 51 deletions(-)

diff --git a/lisp/org-fold-core.el b/lisp/org-fold-core.el
index 23507967f..3fcacb975 100644
--- a/lisp/org-fold-core.el
+++ b/lisp/org-fold-core.el
@@ -1269,41 +1269,42 @@ (defun org-fold-core--fix-folded-region (from to _)
           (let ((new-region (funcall func from to)))
             (setq from (car new-region))
             (setq to (cdr new-region))))
-        (dolist (spec (org-fold-core-folding-spec-list))
-          ;; No action is needed when :fragile is nil for the spec.
-          (when (org-fold-core-get-folding-spec-property spec :fragile)
-            (org-with-wide-buffer
-             ;; Expand the considered region to include partially present fold.
-             ;; Note: It is important to do this inside loop over all
-             ;; specs.  Otherwise, the region may be expanded to huge
-             ;; outline fold, potentially involving majority of the
-             ;; buffer.  That would cause the below code to loop over
-             ;; almost all the folds in buffer, which would be too slow.
-             (let ((local-from from)
-                   (local-to to)
-                   (region-from (org-fold-core-get-region-at-point spec (max (point-min) (1- from))))
-                   (region-to (org-fold-core-get-region-at-point spec (min to (1- (point-max))))))
-               (when region-from (setq local-from (car region-from)))
-               (when region-to (setq local-to (cdr region-to)))
-               (let ((pos local-from))
-                 ;; Move to the first hidden region.
-                 (unless (org-fold-core-get-folding-spec spec pos)
-                   (setq pos (org-fold-core-next-folding-state-change spec pos local-to)))
-                 ;; Cycle over all the folds.
-                 (while (< pos local-to)
-                   (save-match-data ; we should not clobber match-data in after-change-functions
-                     (let ((fold-begin (and (org-fold-core-get-folding-spec spec pos)
-                                            pos))
-                           (fold-end (org-fold-core-next-folding-state-change spec pos local-to)))
-                       (when (and fold-begin fold-end)
-                         (when (save-excursion
-                                 (funcall (org-fold-core-get-folding-spec-property spec :fragile)
-                                          (cons fold-begin fold-end)
-                                          spec))
-                           ;; Reveal completely, not just from the SPEC.
-                           (org-fold-core-region fold-begin fold-end nil)))))
-                   ;; Move to next fold.
-                   (setq pos (org-fold-core-next-folding-state-change spec pos local-to))))))))))))
+        (org-fold-core-cycle-over-indirect-buffers
+          (dolist (spec (org-fold-core-folding-spec-list))
+            ;; No action is needed when :fragile is nil for the spec.
+            (when (org-fold-core-get-folding-spec-property spec :fragile)
+              (org-with-wide-buffer
+               ;; Expand the considered region to include partially present fold.
+               ;; Note: It is important to do this inside loop over all
+               ;; specs.  Otherwise, the region may be expanded to huge
+               ;; outline fold, potentially involving majority of the
+               ;; buffer.  That would cause the below code to loop over
+               ;; almost all the folds in buffer, which would be too slow.
+               (let ((local-from from)
+                     (local-to to)
+                     (region-from (org-fold-core-get-region-at-point spec (max (point-min) (1- from))))
+                     (region-to (org-fold-core-get-region-at-point spec (min to (1- (point-max))))))
+                 (when region-from (setq local-from (car region-from)))
+                 (when region-to (setq local-to (cdr region-to)))
+                 (let ((pos local-from))
+                   ;; Move to the first hidden region.
+                   (unless (org-fold-core-get-folding-spec spec pos)
+                     (setq pos (org-fold-core-next-folding-state-change spec pos local-to)))
+                   ;; Cycle over all the folds.
+                   (while (< pos local-to)
+                     (save-match-data ; we should not clobber match-data in after-change-functions
+                       (let ((fold-begin (and (org-fold-core-get-folding-spec spec pos)
+                                              pos))
+                             (fold-end (org-fold-core-next-folding-state-change spec pos local-to)))
+                         (when (and fold-begin fold-end)
+                           (when (save-excursion
+                                   (funcall (org-fold-core-get-folding-spec-property spec :fragile)
+                                            (cons fold-begin fold-end)
+                                            spec))
+                             ;; Reveal completely, not just from the SPEC.
+                             (org-fold-core-region fold-begin fold-end nil)))))
+                     ;; Move to next fold.
+                     (setq pos (org-fold-core-next-folding-state-change spec pos local-to)))))))))))))
 
 ;;; Handling killing/yanking of folded text
 
diff --git a/lisp/org-fold.el b/lisp/org-fold.el
index ce8afd9b4..6ff21dfc7 100644
--- a/lisp/org-fold.el
+++ b/lisp/org-fold.el
@@ -55,6 +55,7 @@ (defvar org-link-descriptive)
 (defvar org-outline-regexp-bol)
 (defvar org-archive-tag)
 (defvar org-custom-properties-overlays)
+(defvar org-element-headline-re)
 
 (declare-function isearch-filter-visible "isearch" (beg end))
 (declare-function org-element-type "org-element" (element))
@@ -930,6 +931,30 @@ (defun org-fold--extend-changed-region (from to)
   (setq from (save-excursion (goto-char from) (line-beginning-position 0)))
   (cons from to))
 
+(defun org-fold--reveal-headline-at-point ()
+  "Reveal header line and empty contents inside.
+Reveal the header line and, if present, also reveal its contents, when
+the contents consists of blank lines.
+
+Assume that point is located at the header line."
+  (org-with-wide-buffer
+   (beginning-of-line)
+   (org-fold-region
+    (max (point-min) (1- (point)))
+    (let ((endl (line-end-position)))
+      (save-excursion
+        (goto-char endl)
+        (skip-chars-forward "\n\t\r ")
+        ;; Unfold blank lines after newly inserted headline.
+        (if (equal (point)
+                   (save-excursion
+                     (goto-char endl)
+                     (org-end-of-subtree)
+                     (skip-chars-forward "\n\t\r ")))
+            (point)
+          endl)))
+    nil 'headline)))
+
 (defun org-fold--reveal-outline-maybe (region _)
   "Reveal folded outline in REGION when needed.
 
@@ -942,28 +967,25 @@ (defun org-fold--reveal-outline-maybe (region _)
      ;; headline or a list item.
      (backward-char)
      (beginning-of-line)
-     ;; Make sure that headline is not partially hidden
+     ;; Make sure that headline is not partially hidden.
      (unless (org-fold-folded-p nil 'headline)
-       (org-fold-region
-        (max (point-min) (1- (point)))
-        (let ((endl (line-end-position)))
-          (save-excursion
-            (goto-char endl)
-            (skip-chars-forward "\n\t\r ")
-            ;; Unfold blank lines.
-            (if (or (and (looking-at-p "\\*")
-                         (> (point) (1+ endl)))
-                    (eq (point) (point-max)))
-                (point)
-              endl)))
-        nil 'headline))
+       (org-fold--reveal-headline-at-point))
      ;; Never hide level 1 headlines
      (save-excursion
        (goto-char (line-end-position))
        (unless (>= (point) (cdr region))
          (when (re-search-forward (rx bol "* ") (cdr region) t)
-           (org-fold-region (match-beginning 0) (line-end-position) nil 'headline))))
+           (org-fold--reveal-headline-at-point))))
+     ;; Make sure that headline after is not partially hidden.
+     (goto-char (cdr region))
+     (beginning-of-line)
+     (unless (org-fold-folded-p nil 'headline)
+       (when (looking-at-p org-element-headline-re)
+         (org-fold--reveal-headline-at-point)))
      ;; Check the validity of headline
+     (goto-char (car region))
+     (backward-char)
+     (beginning-of-line)
      (unless (let ((case-fold-search t))
 	       (looking-at (rx-to-string
                             `(or (regex ,(org-item-re))
diff --git a/lisp/org.el b/lisp/org.el
index d85b5818e..1c9eaf09a 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -6141,7 +6141,10 @@ (defun org-insert-heading (&optional arg invisible-ok top)
       (unless invisible-ok
         (if (eq org-fold-core-style 'text-properties)
 	    (cond
-	     ((org-fold-folded-p (line-beginning-position) 'headline)
+	     ((org-fold-folded-p
+               (max (point-min)
+                    (1- (line-beginning-position)))
+               'headline)
 	      (org-fold-region (line-end-position 0) (line-end-position) nil 'headline))
 	     (t nil))
           (pcase (get-char-property-and-overlay (point) 'invisible)
-- 
2.35.1


--=-=-=--