Refile and capture to result of a query
I'm not sure if this is in the scope of this project so I'm opening this issue to ask about that.
I have written a first implementation for refiling and capturing to the result of an org-ql query. This is useful for people who want more precise refiling or capturing. You can find the implementation here: https://github.com/Zetagon/literate-dotfiles/blob/master/config.org#refiling .
The code is probably not pull request ready, but if you'd like I can prepare a pull request. Otherwise I'll just leave it here in case somebody in the future looks for this functionality.
There are many possibilities like this that could be enabled by an org-ql query. How best to integrate them is the question. In the future, some kind of org-ql-tools package might be called for. In the meantime, examples like this are probably best shown in documentation.
A few notes about your code:
(defun my/org-refile-to-monthly-review ()
(interactive)
(my/org-refile-to-query '(parent (string-equal (org-id-get) "b5fd67ea-2459-472f-836e-deb113602913"))))
That will be a very slow query to execute, because it will have to call those functions on every heading. Org IDs are properties, so use the property predicate, and it will be many times faster.
As well, it doesn't seem necessary to use a parent query. Put the ID on the heading to which you want to refile the entry, and search for it directly using property. That way the query will return one result: the heading to be refiled to.
(defun my/org-parse-headline ()
"Parse headline at point and put in some more relevant information"
(--> (org-element-headline-parser (line-end-position))
(nth 1 it)
(plist-put it :entry-text
(concat
(buffer-file-name)
":"
(number-to-string (line-number-at-pos))
":"
(buffer-substring (line-beginning-position)
(line-end-position))))
(plist-put it :file-name (buffer-file-name))
(plist-put it :id (org-id-get-create))
(plist-put it :buffer (current-buffer))))
That function should not be necessary for refiling an entry. Look at the results returned by the elements-with-markers action. All you need for the target is the marker.
alphapapa [email protected] writes:
There are many possibilities like this that could be enabled by an
org-qlquery. How best to integrate them is the question. In the future, some kind oforg-ql-toolspackage might be called for. In the meantime, examples like this are probably best shown in documentation.
Ok, that makes sense. I guess I can send a PR to the examples.org file?
A few notes about your code:
Thanks for the feedback!
(defun my/org-refile-to-monthly-review () (interactive) (my/org-refile-to-query '(parent (string-equal (org-id-get) "b5fd67ea-2459-472f-836e-deb113602913"))))That will be a very slow query to execute, because it will have to call those functions on every heading. Org IDs are properties, so use the
propertypredicate, and it will be many times faster.
Ok, I will make sure to update the code.
As well, it doesn't seem necessary to use a
parentquery. Put the ID on the heading to which you want to refile the entry, and search for it directly usingproperty. That way the query will return one result: the heading to be refiled to.
This is actually intentional but it isn't clearly communicated. The entry with the above ID has 12 subtrees, one for every month. The idea is that I can choose which of the subtrees I want to refile to.
That function should not be necessary for refiling an entry. Look at the results returned by the
elements-with-markersaction. All you need for the target is the marker.
I use that information to display text in the `completing-read' prompt. The file name and line number provides important context and keeps the entries unique. This needs to be computed for each matched entry so I think I will keep the function.
Ok, that makes sense. I guess I can send a PR to the examples.org file?
Before submitting a PR, please post the finished code here.
This is actually intentional but it isn't clearly communicated. The entry with the above ID has 12 subtrees, one for every month. The idea is that I can choose which of the subtrees I want to refile to.
I use that information to display text in the `completing-read' prompt. The file name and line number provides important context and keeps the entries unique. This needs to be computed for each matched entry so I think I will keep the function.
Cool, thanks.
FWIW, the refile idea should be covered by org-ql-refile: https://github.com/alphapapa/org-ql/blob/4c1a4b169f54d37ce541902c0ae5043759ef9d9b/org-ql-find.el#L93
A capture command is yet to be implemented, but the Org Capture API doesn't seem naturally suited to it (e.g. there doesn't appear to be a function that takes a marker argument to say "capture to here").
Adam Porter @.***> writes:
A capture command is yet to be implemented, but the Org Capture API doesn't seem naturally suited to it (e.g. there doesn't appear to be a function that takes a marker argument to say "capture to here").
org-capture-templates may have target set to function that moves point to capture location.
-- 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