~protesilaos/denote

1

[PATCH] Fix my-denote-org-extract-subtree section of the README

Details
Message ID
<CABzEscbPx24LCUCc7JsMmQtVGwhou5fUH_5h+=t=qi4396NqNQ@mail.gmail.com>
DKIM signature
pass
Download raw message
From 21056f63c9b9be9e9adcc53ccf07aa31e7534f28 Mon Sep 17 00:00:00 2001
From: Vedang Manerikar <ved.manerikar@gmail.com>
Date: Thu, 1 Jun 2023 09:23:40 +0530
Subject: [PATCH] Fix my-denote-org-extract-subtree section of the README

The provided function did not work correctly. This commit fixes the
following issues:

1. Fix: Extract tags before deleting the region from the source file.
2. Fix: Use `org-end-of-subtree` to calculate the point we should
   delete upto. `org-entry-end-position` ends at the first sub-heading
   under the tree, which is not what we want. Instead, we want to cut
   the whole subtree.
3. Enhance: Retain the date information available in the subtree. We
   look for three common places for this information -- The CREATED or
   DATE properties in the PROPERTIES drawer, and the CLOSED cookie at
   the element level itself.
---
 README.org | 46 +++++++++++++++++++++++++++++-----------------
 1 file changed, 29 insertions(+), 17 deletions(-)

diff --git a/README.org b/README.org
index 85b0558..1a5c745 100644
--- a/README.org
+++ b/README.org
@@ -2304,23 +2304,35 @@ file.  The contents of the subtree become the
contents of the new note
 and are removed from the old one.

 #+begin_src emacs-lisp
-(defun my-denote-org-extract-subtree ()
-  "Create new Denote note using current Org subtree.
-Make the new note use the Org file type, regardless of the value
-of `denote-file-type'.
-
-Use the subtree title as the note's title.  If available, use the
-tags of the heading are used as note keywords.
-
-Delete the original subtree."
-  (interactive)
-  (if-let ((text (org-get-entry))
-           (heading (org-get-heading :no-tags :no-todo :no-priority
:no-comment)))
-      (progn
-        (delete-region (org-entry-beginning-position) (org-entry-end-position))
-        (denote heading (org-get-tags) 'org)
-        (insert text))
-    (user-error "No subtree to extract; aborting")))
+  (defun my-denote-org-extract-subtree ()
+    "Create new Denote note using current Org subtree.
+  Make the new note use the Org file type, regardless of the value
+  of `denote-file-type'.
+
+  Use the subtree title as the note's title.  If available, use the
+  tags of the heading are used as note keywords.
+
+  Delete the original subtree."
+    (interactive)
+    (if-let ((text (org-get-entry))
+             (heading (org-get-heading :no-tags :no-todo :no-priority
:no-comment)))
+        (let ((element (org-element-at-point))
+              (tags (org-get-tags)))
+          (delete-region (org-entry-beginning-position)
+                         (save-excursion (org-end-of-subtree t) (point)))
+          (denote heading
+                  tags
+                  'org
+                  nil
+                  (or
+                   ;; Check PROPERTIES drawer for :created: or :date:
+                   (org-element-property :CREATED element)
+                   (org-element-property :DATE element)
+                   ;; Check the subtree for CLOSED
+                   (org-element-property :raw-value
+                                         (org-element-property
:closed element))))
+          (insert text))
+      (user-error "No subtree to extract; aborting")))
 #+end_src

 Have a different workflow?  Feel welcome to discuss it in any of our
-- 
2.40.1
Details
Message ID
<87pm6fvy3c.fsf@protesilaos.com>
In-Reply-To
<CABzEscbPx24LCUCc7JsMmQtVGwhou5fUH_5h+=t=qi4396NqNQ@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
> From: Vedang <ved.manerikar@gmail.com>
> Date: Thu,  1 Jun 2023 09:56:09 +0530
>
> From 21056f63c9b9be9e9adcc53ccf07aa31e7534f28 Mon Sep 17 00:00:00 2001
> From: Vedang Manerikar <ved.manerikar@gmail.com>
> Date: Thu, 1 Jun 2023 09:23:40 +0530
> Subject: [PATCH] Fix my-denote-org-extract-subtree section of the README

> [... 78 lines elided]

Thank you Vedang!  I installed your patch and pushed the changes.  I
also updated the manual to include your name in the "Acknowledgements"
section.

-- 
Protesilaos Stavrou
https://protesilaos.com
Reply to thread Export thread (mbox)