I am looking for a way to programmatically search for headings in the current subtree. More specifically, I have a vocabulary file, in which I store words I need to learn. Each word is a subtree in itself and has the property
Interactively I would usually do the following:
C-c a < < m
- Enter my search query, something like:
And I would get the vocabulary that I did not yet mark "done" and that I practiced before some date, according to the property (for which I have a custom procedure for updating, which I mapped to
l for "last").
Here is an example of what a part of my document looks like:
* HSK 3 :hsk_3:3: ** well known :wellknown: *** Words **** DONE bike / bicycle :PROPERTIES: :LAST_PRACTICED_DATE: [2023-10-01 Sun] :END: ***** zìxíngchē ****** 自行车 **** VERIFY easy :PROPERTIES: :LAST_PRACTICED_DATE: [2023-10-02 Mon] :END: ***** róngyì ****** 容易 ** somewhat known :somewhatknown: (more words here!) ** not known :notknown: (more words here!)
VERIFY is a TODO status. I specified it at the top of the file.)
Now I would like to put all of that into an elisp source block instead.
Here is what I have come up with so far:
(save-excursion (save-restriction (org-previous-visible-heading 1) (let ((elem (org-element-at-point))) (let ((start (org-element-property :begin elem)) (end (org-element-property :end elem))) (message "start at: " (number-to-string start)) (message "end at: " (number-to-string end)) (narrow-to-region start end) (org-tags-view nil "LAST_PRACTICED_DATE<\"<2023-10-01 Sun>\"-TODO=\"DONE\"")))))
This I put in an elisp source block directly under the heading
well known, so that it should narrow the search to that subtree.
However, when I run that, the search is not narrowed to the subtree, but instead searches the whole document or at least a higher level subtree instead and shows results from
somewhat known or
not known. Also maybe noteworthy is, that the calls to
(message ...) result in only the text but no numbers being printed in the
*Messages* buffer. But I don't know how to inspect the
end values in other ways or how to print the whole
I thought this code would work, because I found some snippet in the code of
(save-excursion (save-restriction (if (eq buffer org-agenda-restrict) (narrow-to-region org-agenda-restrict-begin org-agenda-restrict-end) (widen)) (setq rtn (org-scan-tags 'agenda matcher org--matcher-tags-todo-only)) (setq rtnall (append rtnall rtn))))
So I thought narrowing before matching would do the trick.
It seems I am doing something wrong though.
How can I implement this procedure?