Ihor Radchenko <yanta...@posteo.net> writes:

> Konstantin Borisov <ir...@ya.ru> writes:
>
>> Currently there is a possibility to use `:comments noweb' header option
>> in src block, that adds comments with links to source block to the
>> tangled files. And this option works great with `org-babel-detangle' in
>> simple cases, but it fails to work in more complex ones.
>> …
>
> This is a known problem.
> See 
> <https://list.orgmode.org/orgmode/1513516023.10763084.1682269758...@mail.yahoo.com/>
>

Ahh.. Ok. Thank you. I checked the tracker.orgmode.org and did not find
it there. Will check the mail list archive for information too.

>> I also propose to not use links for blocks reference, but line-numbers
>> of where block starts in org-file. As far as it’s not possible to fill
>> the same tangled file from different org-files, I propose to make
>> special comment line with the file where it was tangled from. Thus
>> comments will be shorter and easier to view (org-links not working in
>> source files anyway).
>
> Using line numbers is not exactly reliable. What if the source org file
> changes? The line numbers will not be correct then.
>

>From the theoretical perspective of view you are right. But from the
practical I think line numbers should work well enough.

Also we could use line-numbers as fallback. It the block is named then
it’s ok to use it’s name directly, but when not, line-numbers will work
better than current implementation with fuzzy line search, all not named
source blocks looks very similar (especially when move `:noweb-ref'
header argument to `:header-args:' section property).

I can see following practical cases when detangle could be useful:

• automatic prettifiers
  for now prettifiers are working well in special-edit buffer, but them
  using the line width limits ignoring the noweb prefixes and when tangled
  we could receive lines longer then we want, so it could be useful to
  prettify the whole tangled file and then detangle sources back
• little hotfixes
  sometimes debugging in tangled file is easier than in org-sources, so it
  is easier to fix some typo or little bug right in tangled file, than
  navigate to org-sources and fix it
• work with lsp
  it requires the whole files for good work and it could do some changes
  like rename or something, so it could be useful to be able to get those
  fixes back to org-sources
• statical analyzers (linters)
  that could do some fixes in tangled files too, and it would be useful
  to getting those changes back to org-sources automatically

All these cases use tangled source files as temporary solution, so no big
problem to regenerate them when something changed in org-source file.
In the worst case detangle function will not find the source block on
specified line number and will fire “Not in tangled code” error.

Personally I’m using the `org-auto-tangle' package to regenerate tangled
sources automatically when org-source changed and saved and it’s working
very well, so my line-numbers will be fresh enough for hot-fixes in
tangled files.

>> …
>> In comments it have following information:
>> - where to find the org-file this file was tangled from (`ORG-FILE
>>   filepath')
>> - where to find source block with sources (`SRC line-in-org-file' and
>>   `SRC line-in-org-file ends here') 
>> - the borders of noweb references (`NOWEB noweb-ref' and `NOWEB
>>   noweb-ref ends here') 
>> …
>> To acheive such result we need to modify the
>> `org-babel-expand-noweb-reference' function to not just insert the
>> source blocks links, but also adding the noweb reference begin/end
>> comments lines to tangled file.
>>
>> Detangle function `org-babel-detangle' with such structure of comments
>> will have enough information to properly detangle source blocks using
>> the following algorithm: 
>>
>> 1. Load tangled file content to temporary buffer
>> 2. Find first source block that do not have any comment lines started
>>    with `SRC' and `NOWEB' (the leaves of noweb source blocks tree) inside. 
>> 3. If found
>>   3.1. detangle that source block and remove it from processing buffer
>>   3.2. go to step 2.
>> 4. If not found
>>   4.1. find the pair of comments started with `NOWEB' and apropriate
>>        noweb reference
>>   4.2. If found
>>     4.2.1. it should be empty between those comments
>>     4.2.2. replace those comments lines with the apropriate noweb
>>            reference code 
>>     4.2.3. go to step 2.
>>   4.3. If not found
>>     4.3.1. Complete
>> …
>
> I believe that the ultimate solution will be using
> <https://leo-editor.github.io/leo-editor/appendices.html#the-mulder-ream-update-algorithm>

Probably you are right. From the first glance their detangle works, but
looks like them using some simpler noweb version. And btw them stil
using `noweb-ref' marks in tangled code even if not using them for detangle.
Here is how their tangled file looks like.

┌────
│ #@+leo-ver=5-thin
│ #@+node:irbis.20250803220254.1: * @file test.py
│ #@@language python
│ #@@tabwidth -4
│ print("before")
│ #@+<<section>>
│ #@+node:irbis.20250803224033.1: ** <<section>>
│ print("section")
│ #@+<<subsection>>
│ #@+node:irbis.20250803225404.1: *3* <<subsection>>
│ print("sub")
│ #@-<<subsection>>
│ print("more")
│ #@-<<section>>
│ print("after")
│ print("more")
│ #@-leo
└────

–
Looking forward for your message,
Konstantin.

Reply via email to