Re: Is it possible to #+include: src blocks and tangle them too?
On 2021-04-12 23:38, Nicolas Goaziou wrote: > However, you may try calling `org-export-expand-include-keyword' from > `org-babel-pre-tangle-hook'. Untested. I've done this in the past and can verify that it works well. That said, it by definition creates cross-talk between org files, with all the potential for name collisions and the like that that implies. One could avoid such problems by adding some kind of prefix to the block names in the included file (maybe "common/" or "xyzzy-"). (Some kind of automatic prefixing on inclusion might be cool.) --Kevin M. Stout
Re: How do you name your code blocks?
On 2021-02-15 14:18, Rodrigo Morales wrote: > 1. Do you use long names? Usually. Suppose you were doing a bit of genetic programming. You might have a function that computes the next generation from the current one. You could write the following snippet that looks suspiciously like pseudocode: #+HEADER::noweb-ref genetic/functions #+BEGIN_SRC python def next_generation(curr): <> <> <> <> return new #+END_SRC You would then have a block dedicated to each major part of the evolutionary step. > 2. If not, how do you name your code blocks to avoid name conflicts? I have been over the tangling and noweb expansion code quite a bit lately. The chief benefit of using #+NAME on a code block is ability to call it. For example, you might have #+NAME: generate keymap #+BEGIN_SRC elisp :var t=keymap ... #+END_SRC whose sole purpose is to take a table from elsewhere in an Org file and generate the code that sets up a keymap. To use it, you might say #+BEGIN_SRC elisp <> #+END_SRC For every other purpose, :noweb-ref works better. In newer versions of org, it's the sole means of accumulating code under a common name in the WEB/Noweb tradition. In older versions, duplicate #+NAMEs did result in accumulation, but the behavior was undefined. A word on syntax: I find #+BEGIN_SRC language :noweb-ref "block name" ... #+END_SRC less readable than #+HEADER::noweb-ref block name #+BEGIN_SRC language ... #+END_SRC especially when there are further block-specific header args. Either is more cumbersome to type than the #+NAME syntax, but that can be dealt with using something like (add-to-list 'org-structure-template-alist '("ss" "#+HEADER::noweb-ref ?\n#+BEGIN_SRC\n\n#+END_SRC")) Then, to set up a safely-named block,
Re: 2 Surprises and 2 Questions Regarding Org Tangle
On 2021-02-11 18:44, Diego Zamboni wrote: > #2 is known (maybe documented? Not sure) behavior: using :noweb-ref > accumulates multiple blocks with the same name, whereas #+NAME uses only > the first one. I think #+NAME's are supposed to be unique within a document. It seems that more recent versions of org-babel-expand-noweb-references (not org-babel-tangle, as I said in an earlier message) no longer accumulate blocks with common #+NAMEs. (Now, though, #+NAME has priority over :noweb-ref, which is the opposite of how things were done before.) Looking through some older discussions on the topic, I do see the downsides of accepting duplicate #+NAMEs, which are rooted in the assumption of uniqueness made elsewhere. Even so, I still hold that #+NAME: this is a cool way to do something #+BEGIN_SRC language stuff #+END_SRC is a much nicer syntax than #+BEGIN_SRC language :noweb-ref "this is a cool way to do something" stuff #+END_SRC especially as a project grows. Perhaps we could have a variation on #+NAME that doesn't need to be unique? Maybe something like #+NOREF, to evoke :noweb-ref? Or #+NONAME? Sincerely, Kevin M. Stout
Re: 2 Surprises and 2 Questions Regarding Org Tangle
On 2021-02-11 18:44, Diego Zamboni wrote: > #2 is known (maybe documented? Not sure) behavior: using :noweb-ref > accumulates multiple blocks with the same name, whereas #+NAME uses only > the first one. Deep in org-babel-tangle, (org-babel-map-src-blocks nil (let ((i (let ((org-babel-current-src-block-location (point))) (org-babel-get-src-block-info 'light (when (equal (or (cdr (assq :noweb-ref (nth 2 i))) (nth 4 i)) source-name) ...))) So, while :noweb-ref gets priority over #+NAME, we can still accumulate blocks having the same #+NAME (provided there's no overriding :noweb-ref). > I think #+NAME's are supposed to be unique within a document. Even though I'm a huge fan of this behavior, which lines up well with the traditional WEB and Noweb behavior, the Org manual's discussion of code blocks does say, "For duplicate names, Org mode's behavior is undefined." [Section 14.1] So, Dear Org Maintainers, Please do not change org-babel-tangle's accumulation behavior for duplicate #+NAME's. It's handy, and users of other literate programming tools will find it familiar. Also, #+NAME is a nicer per-block syntax than :noweb-ref. Sincerely, Kevin M. Stout