From 7f5e085cad95b5ed51da872ab5f136ff21217e50 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Mon, 5 Feb 2024 14:04:52 +0100 Subject: [PATCH] org-decrypt-entry: Adjust headline levels in decrypted subtree * lisp/org-crypt.el (org-decrypt-entry): When decrypted subtree contains headings with levels below the containing heading, demote that subtree, so that originally encrypted text remains under the same parent heading. This is useful when encrypted heading is promoted. --- lisp/org-crypt.el | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lisp/org-crypt.el b/lisp/org-crypt.el index e57ded609..c8184ee95 100644 --- a/lisp/org-crypt.el +++ b/lisp/org-crypt.el @@ -251,6 +251,7 @@ Assume `epg-context' is set." (org-fold-subtree t)) nil))))) +(defvar org-outline-regexp-bol) ;;;###autoload (defun org-decrypt-entry () "Decrypt the content of the current headline." @@ -266,6 +267,7 @@ Assume `epg-context' is set." (save-excursion (org-previous-visible-heading 1) (point)))) + (level (org-current-level)) (encrypted-text (org-crypt--encrypted-text beg end)) (decrypted-text (decode-coding-string @@ -276,15 +278,27 @@ Assume `epg-context' is set." ;; 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 - ;; process. - (insert "\n" - (propertize decrypted-text - 'org-crypt-checksum (sha1 decrypted-text) - 'org-crypt-key (org-crypt-key-for-heading) - 'org-crypt-text encrypted-text)) + (if (string-match (org-headline-re level) decrypted-text) + ;; If decrypted text contains other headings with levels + ;; below LEVEL, adjust the subtree. + (let ((start 0) (min-level level)) + (while (string-match (org-headline-re level) decrypted-text start) + (setq min-level (min min-level (1- (length (match-string 0 decrypted-text)))) + start (match-end 0))) + (insert "\n" + (replace-regexp-in-string + org-outline-regexp-bol + (concat (make-string (1+ (- level min-level)) ?*) "\\&") + decrypted-text))) + ;; 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 + ;; process. + (insert "\n" + (propertize decrypted-text + '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))