emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] org-crypt-decrypt-entry: Apply initial visibility upon decryption
@ 2023-04-30 20:21 Ihor Radchenko
  2023-05-01  3:41 ` [SUGGESTION] " Christopher M. Miles
                   ` (2 more replies)
  0 siblings, 3 replies; 29+ messages in thread
From: Ihor Radchenko @ 2023-04-30 20:21 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 219 bytes --]

Hi,

I'd like to propose applying initial visibility to the freshly decrypted
headings. IMHO, it makes much more sense by default compared to showing
all the text unfolded, including property drawers.

Any objections?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-crypt-decrypt-entry-Apply-initial-visibility-upo.patch --]
[-- Type: text/x-patch, Size: 4789 bytes --]

From 5f90460cf565e9c3185f58a631500207e9b14d39 Mon Sep 17 00:00:00 2001
Message-Id: <5f90460cf565e9c3185f58a631500207e9b14d39.1682886052.git.yantar92@posteo.net>
From: Ihor Radchenko <yantar92@posteo.net>
Date: Sun, 30 Apr 2023 22:15:11 +0200
Subject: [PATCH] org-crypt-decrypt-entry: Apply initial visibility upon
 decryption

* lisp/org-crypt.el: Call `org-cycle-set-startup-visibility' on the
decrypted entries.  Still re-fold if the decrypted entry has been
inside an existing fold.
* lisp/org-cycle.el (org-cycle-set-visibility-according-to-property):
Respect narrowing.
* etc/ORG-NEWS (=org-crypt.el= now applies initial visibility settings
to decrypted entries): Announce the change.

This patch solves a slight annoyance when the freshly decrypted entry
is fully unfolded, including all the property drawers.  It will make
more sense to make the folding state follow initial visibility
settings, including VISIBILITY properties.
---
 etc/ORG-NEWS      |  4 ++++
 lisp/org-crypt.el | 11 ++++++++++-
 lisp/org-cycle.el | 15 ++++++++-------
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 03894f128..70e684d10 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -214,6 +214,10 @@ execution completes.  The new ~:async~ header allows users to continue
 editing with Emacs while a ~:session~ block executes.
 
 ** Miscellaneous
+*** =org-crypt.el= now applies initial visibility settings to decrypted entries
+
+Previously, all the text was unfolded unconditionally, including property drawers.
+
 *** Blank lines after removed objects are not retained during export
 
 When certain objects in Org document are to be excluded from export,
diff --git a/lisp/org-crypt.el b/lisp/org-crypt.el
index b5df115cf..39d1a421d 100644
--- a/lisp/org-crypt.el
+++ b/lisp/org-crypt.el
@@ -81,6 +81,7 @@ (declare-function org-make-tags-matcher "org" (match))
 (declare-function org-previous-visible-heading "org" (arg))
 (declare-function org-scan-tags "org" (action matcher todo-only &optional start-level))
 (declare-function org-set-property "org" (property value))
+(declare-function org-cycle-set-startup-visibility "org-cycle" ())
 
 (defgroup org-crypt nil
   "Org Crypt."
@@ -269,10 +270,12 @@ (defun org-decrypt-entry ()
 	      (decrypted-text
 	       (decode-coding-string
 		(epg-decrypt-string epg-context encrypted-text)
-		'utf-8)))
+		'utf-8))
+              origin-marker)
 	 ;; Delete region starting just before point, because the
 	 ;; outline property starts at the \n of the heading.
 	 (delete-region (1- (point)) end)
+         (setq origin-marker (point-marker))
 	 ;; Store a checksum of the decrypted and the encrypted text
 	 ;; value.  This allows reusing the same encrypted text if the
 	 ;; text does not change, and therefore avoid a re-encryption
@@ -282,6 +285,12 @@ (defun org-decrypt-entry ()
 			     'org-crypt-checksum (sha1 decrypted-text)
 			     'org-crypt-key (org-crypt-key-for-heading)
 			     'org-crypt-text encrypted-text))
+         ;; Apply initial visibility.
+         (save-restriction
+           (narrow-to-region origin-marker (point))
+           (set-marker origin-marker nil)
+           (org-cycle-set-startup-visibility))
+         ;; ... but keep the previous folded state.
 	 (when folded-heading
 	   (goto-char folded-heading)
 	   (org-fold-subtree t))
diff --git a/lisp/org-cycle.el b/lisp/org-cycle.el
index 50aa0d21d..44e015746 100644
--- a/lisp/org-cycle.el
+++ b/lisp/org-cycle.el
@@ -638,20 +638,21 @@ (defun org-cycle-set-visibility-according-to-property ()
   "Switch subtree visibility according to VISIBILITY property."
   (interactive)
   (let ((regexp (org-re-property "VISIBILITY")))
-    (org-with-point-at 1
+    (save-restriction
+      (goto-char (point-min))
       (while (re-search-forward regexp nil t)
-	(let ((state (match-string 3)))
+        (let ((state (match-string 3)))
 	  (if (not (org-at-property-p)) (outline-next-heading)
 	    (save-excursion
 	      (org-back-to-heading t)
 	      (org-fold-subtree t)
 	      (pcase state
-		("folded"
+	        ("folded"
 		 (org-fold-subtree t))
-		("children"
+	        ("children"
 		 (org-fold-show-hidden-entry)
 		 (org-fold-show-children))
-		("content"
+	        ("content"
                  ;; Newline before heading will be outside the
                  ;; narrowing.  Make sure that it is revealed.
                  (org-fold-heading nil)
@@ -659,9 +660,9 @@ (defun org-cycle-set-visibility-according-to-property ()
 		   (save-restriction
 		     (org-narrow-to-subtree)
 		     (org-cycle-content))))
-		((or "all" "showall")
+	        ((or "all" "showall")
 		 (org-fold-show-subtree))
-		(_ nil)))
+	        (_ nil)))
 	    (org-end-of-subtree t)))))))
 
 (defun org-cycle-overview ()
-- 
2.40.0


[-- Attachment #3: Type: text/plain, Size: 224 bytes --]


-- 
Ihor Radchenko // yantar92,
Org mode contributor,
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>

^ permalink raw reply related	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2023-05-15 16:05 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-30 20:21 [PATCH] org-crypt-decrypt-entry: Apply initial visibility upon decryption Ihor Radchenko
2023-05-01  3:41 ` [SUGGESTION] " Christopher M. Miles
2023-05-01  7:50   ` Ihor Radchenko
2023-05-01 11:32     ` Christopher M. Miles
2023-05-01 11:46       ` Ihor Radchenko
2023-05-01 12:26         ` Christopher M. Miles
2023-05-01 12:38           ` Ihor Radchenko
2023-05-01 13:12             ` Christopher M. Miles
2023-05-01 13:38               ` Ihor Radchenko
2023-05-01 19:10                 ` Christopher M. Miles
2023-05-01 19:18                   ` Ihor Radchenko
     [not found]             ` <m2sfcgmbpo.fsf@numbchild>
2023-05-02  4:00               ` [EasyPG (epa)] Emacs can't save modified encrypted file Christopher M. Miles
2023-05-02 11:57                 ` Ihor Radchenko
2023-05-02 17:38                   ` Christopher M. Miles
2023-05-02 20:08                     ` Ihor Radchenko
2023-05-03 10:40                       ` Christopher M. Miles
2023-05-03 12:08                         ` Ihor Radchenko
2023-05-03 16:51                           ` Christopher M. Miles
2023-05-03 17:02                             ` Ihor Radchenko
2023-05-03 17:29                               ` [Uploaded Edebug video] " Christopher M. Miles
2023-05-03 19:08                                 ` Ihor Radchenko
2023-05-04  2:28                                   ` Christopher M. Miles
2023-05-04 17:28                                     ` Ihor Radchenko
2023-05-05  6:30                                       ` Christopher M. Miles
2023-05-05  8:36                                         ` Ihor Radchenko
2023-05-05 13:20                                           ` Christopher M. Miles
2023-05-05 15:29                                           ` Christopher M. Miles
2023-05-15 13:38 ` [PATCH] org-crypt-decrypt-entry: Apply initial visibility upon decryption Ihor Radchenko
2023-05-15 15:56 ` Christopher M. Miles

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).