-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transclusion of last entry of subtree only #247
Comments
One trivial way I could suggest is using the |
Hi @akashpal-21 , Thank you for your suggestion, but I am looking for a solution that dynamically finds the last entry (perhaps triggered by some refresh, turning org-transclude-mode on and off, or something). For example, if I update my above example file to
I want the transclude to pick that up automatically and display
I have attempted some solutions with custom functions that find the last subtree in a given heading, and then generate the entire transclude statement (including the last header, hardcoded), but that still required a lot of manual work (i.e. removing the previously generated ones). I think this has to be some function within org-transclude to work nicely, but i may be wrong here. |
I think you are right. We don't have an easy way within Org-transclusion at the moment as far as I remember/tried. What's the function you use to dynamically find the last tree in a given heading? I don't think I can do much until mid-late June, but I am considering something that may help with this. |
hi @nobiot With the help of chatGPT I created a function that given a header, finds the last subheader (optionally given a maximum allowed depth). A second function then builds up the transclude statement. The two functions are as follows (defun my/find-last-entry-in-subtree (file subtree-heading &optional max-depth)
"Find the last entry in the subtree under SUBTREE-HEADING from FILE.
Print and return the heading of the last entry. If MAX-DEPTH is provided,
limit the search to that depth."
(let ((last-heading nil)
(max-depth (or max-depth most-positive-fixnum)))
(with-current-buffer (find-file-noselect file)
(goto-char (point-min))
(if (re-search-forward (format "^\\*+ %s" (regexp-quote subtree-heading)) nil t)
(let ((start-level (org-current-level)))
(message "Subtree '%s' found at level %d." subtree-heading start-level)
(forward-line) ; Move to the next line to start checking for headings
(while (and (re-search-forward org-heading-regexp nil t)
(> (org-current-level) start-level)) ; Ensure we stay within the subtree
(let ((current-level (org-current-level)))
(when (and (<= current-level (+ start-level max-depth))
(> current-level start-level))
(setq last-heading (org-element-property :raw-value (org-element-at-point)))
(message "Updated last heading to: %s at level %d" last-heading current-level))))
(if last-heading
(message "Last entry heading within depth %d: %s" max-depth last-heading)
(message "No entry found within depth %d under subtree '%s'." max-depth subtree-heading)))
(message "Subtree '%s' not found." subtree-heading)))
last-heading))
(defun my/org-transclude-last-entry (file subtree-heading &optional max-depth)
"Transclude the last entry of a subtree under SUBTREE-HEADING from FILE.
If MAX-DEPTH is provided, limit the search to that depth."
(let ((last-heading (my/find-last-entry-in-subtree file subtree-heading max-depth)))
(if last-heading
(let ((transclude-command (format "#+transclude: [[file:%s::*%s]]\n" file last-heading)))
(message "Transclude command: %s" transclude-command)
(insert transclude-command))
(message "No last entry found in subtree '%s' within depth %d." subtree-heading max-depth)))) I suppose that first function could be used somewhere in org-transclusion. I should say, my lisp knowledge is little and there may be things wrong with this function, so beware using it blindly. |
What I had in mind and tested a little is simple. I assume that the structure of the journal in your example above is fixed -- ie the date entry is always level 2. Then you can simply evaluate this function and add a custom org link. (defun my/org-last-entry (path arg)
(org-link-open-as-file path arg)
(goto-char (car (last (org-map-entries #'point "level=2" 'file)))))
(org-link-set-parameters "last-entry" :follow #'my/org-last-entry) It enables this
I wanted to create a new wrapper function within Org-transclusion that can transclude an arbitary link like this. The idea is, if the link can navigate to the target, then it can also be turned into a transclusion by simply adding Would any part of the idea above work for your case? I am not yet certain if enabling transclusion for any arbitrary link will actually work technically -- but this is something I am thinking of trying in late June (in addition to continuation of some bug fixes). |
Hi @nobiot, apologies for the late reply. Thank you for looking into this a bit already and thinking along.
the situation is not entirely like this but:
Given the above considerations, if we would be able to:
The code I shared did more or less that. In your example,
it doesnt look like it's easy to provide additional configurations, whereas there already exists some things that look relevant and maybe have a better way to provide these configurations?
if we could just add something like I have no clue if there is anything preventing something like this, though. |
@nobiot have you had the chance to look at any of this? I will pick up trying some things out in the coming weeks again |
Yes looked at your suggestion. There are ways to do this but not directly in the way you suggested. I am specifically referring to this syntax: #+transclude: [[file:path/to/file.org::*Headline]] :level 2 :level property is already used by org-transclusion for a different purpose. I can make more nuanced comments about what would work but I cannot elaborate right now -- would need to come back, later today or this week. Sorry a little hectic these past few weeks outside Emacs. |
Try the following - we cannot use level because it is used up to mean the output level of the transcluded content Since Nobiot is working on a better name - for the time being - you can use :headlinelevel to mean it -- I present the basic changes you'd need to make to implement it inside the transclusion protocol
Tested lightly. |
Introduces a nasty regression in that it breaks :level -- we have to reset the markers relative to the level and inverse the timing of operations between :level and :headlinelevel
Will update the above snippet |
@akashpal-21, thank you for the code. It has given me some better ideas. Original idea -- prefer generic solutionUsing your suggest as an example (adding @akashpal-21's change):
This, to me, extends Org's built-in link functionality. This extension is specific to your use case. Where possible, I prefer to try to work out a more generic way to achieve the same end result. One idea was to use the Org's built-in feature to create your own custom links, like this.
If you can create a custom link that lets you specify the subtree and level, for example (hypothetical custom link just for illustration):
This can be more generic functionality, and hence this idea I suggested below.
If implemented, it would let you use the following transclusion syntax (and, in theory, any custom links that you may create).
Specific solution -- extending @akashpal-21's ideaThis would be a customizing. I think you could avoid overriding the functions. A couple of suggestions:
For example, it may make sense to have a filter like
The challenge for me is that currently it's not easy to add a new custom filter as a customizing. I think I have made some attempt in the course of discussing this PR -- I remember I refactored relevant parts, but never merged this to main. Back to original requestIn either of the 2 options above, it will take some efforts. |
The other problem is that there is not an easy way to make all the other elements play nice. We could work around it using a slightly more complication involving org-element-map and making some more adjustment, but this adds way too much complexity in the end to allow for a niche usecase. Using custom links make more sense |
LS,
My usecase is a technical report/overview of projects and I would like to transclude the last subtree of a particular tree. For each project I keep a journal and in a particular overview node I want to automatically transclude all the most recent project journal-entries. I have looked around in the documentation, but it did not become entirely clear to me whether this is actually possible and how I would do this.
MWE:
when pointing to a tree
I want the transclusion to show me only
optionally without the subheader
Is this possible? sounds like it fits the usecase listed in the docs.
Kind regards,
David
The text was updated successfully, but these errors were encountered: