Jia,

#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.

I don't know about #1, the output from your P1 example seems surprising to me as well.

--Diego



On Thu, Feb 11, 2021 at 5:13 PM Lee Jia Hong <jia_hong@live.com.my> wrote:
(First time posting to a mailing list, please correct me if I did something wrong.)

TLDR:
Surprise 1: Different Noweb reference placing styles produces different tangled results.
Question 1: Is it a bug?

Surprise 2: Source block naming with #+NAME: and :noweb-ref produce different tangled results.
Question 2: Is it a bug?
------------------------------------------------------------------------------------

Hi,

I stumble upon this article (https://www.hhyu.org/posts/literate_config/), and there are two things that standout:
1. the way author places the Noweb reference, i.e.:
  (setq org-capture-templates
      '(
        <<ORG_CAPTURE>>
       )
  )
2. the way he uses :noweb-ref to tangle multiple code blocks to the same Noweb reference.



* Surprise 1: Different Noweb Reference Placing Styles Produces Different Tangled Results
------------------------------------------------------------------------------------------------
The way he places the Noweb reference intrigues me, so I try writing it in different (placing) styles:
P1:
(setq org-capture-templates '(<<ORG_CAPTURE>>))

P2:
(setq org-capture-templates
      '(<<ORG_CAPTURE>>))

P3:
(setq org-capture-templates
      '(
        <<ORG_CAPTURE>>))

P4:
(setq org-capture-templates
      '(
        <<ORG_CAPTURE>>
        ))

Tangled results using P3 and P4 styles match my expectations. But the results of P1 and P2 styles surprise me.

Expected result for P1:
#+begin_example
(setq org-capture-templates '(("t" "TODO inbox"
                                   entry
                                   (file "~/gtd/capture.org")
                                   "* TODO %?
                                   SCHEDULED: %t")
                              ("n" "notes inbox"
                                   entry
                                   (file "~/gtd/inbox.org")
                                   "* %T\n%i%?")))
#+end_example

Actual result:
#+begin_example
(setq org-capture-templates '(("t" "TODO inbox"
(setq org-capture-templates '( entry
(setq org-capture-templates '( (file "~/gtd/capture.org")
(setq org-capture-templates '( "* TODO %?
(setq org-capture-templates '(    SCHEDULED: %t")
(setq org-capture-templates '(("n" "notes inbox"
(setq org-capture-templates '( entry
(setq org-capture-templates '( (file "~/gtd/inbox.org")
(setq org-capture-templates '( "* %T\n%i%?")))
#+end_example

Expected result for P2:
#+begin_example
(setq org-capture-templates
      '(("t" "TODO inbox"
         entry
         (file "~/gtd/capture.org")
         "* TODO %?
            SCHEDULED: %t")
        ("n" "notes inbox"
         entry
         (file "~/gtd/inbox.org")
         "* %T\n%i%?")))
#+end_example

Actual result:
#+begin_example
(setq org-capture-templates
      '(("t" "TODO inbox"
      '( entry
      '( (file "~/gtd/capture.org")
      '( "* TODO %?
      '(    SCHEDULED: %t")
      '(("n" "notes inbox"
      '( entry
      '( (file "~/gtd/inbox.org")
      '( "* %T\n%i%?")))
#+end_example

Question 1: Is this a bug? If not, how can I make sure that style P1 and P2 produce the expected results?



* Surprise 2: Source Block Naming with #+NAME: and :noweb-ref Produce Different Tangled Results
-----------------------------------------------------------------------------------------------------------
I have been naming my source blocks with #+NAME, and it's my first time realising that it's possible to tangle multiple source blocks to a single Noweb reference. To try it out, I write the reference holder like this:
#+begin_src emacs-lisp
(setq org-capture-templates
      '(
        <<ORG_CAPTURE>>))
#+end_src
(Note: Use placing style P3 because styles P1 and P2 doesn't work as expected, see Surprise 1 above)

Then follow by source blocks named with #+NAME:
#+NAME: ORG_CAPTURE
#+begin_src emacs-lisp
("t" "TODO inbox"
 entry
 (file "~/gtd/capture.org")
 "* TODO %?
    SCHEDULED: %t")
#+end_src

#+NAME: ORG_CAPTURE
#+begin_src emacs-lisp
("n" "notes inbox"
 entry
 (file "~/gtd/inbox.org")
 "* %T\n%i%?")
#+end_src

Expected result:
#+begin_example
(setq org-capture-templates
      '(
        ("t" "TODO inbox"
         entry
         (file "~/gtd/capture.org")
         "* TODO %?
            SCHEDULED: %t")
        ("n" "notes inbox"
         entry
         (file "~/gtd/inbox.org")
         "* %T\n%i%?")))
#+end_example

Actual result:
#+begin_example
(setq org-capture-templates
      '(
        ("t" "TODO inbox"
         entry
         (file "~/gtd/capture.org")
         "* TODO %?
            SCHEDULED: %t")))
#+end_example
(Not working as expected, because it only tangle the first source block with the given name.)

However, with :noweb-ref <<REFERENCE>>, it's working as expected:
#+begin_src emacs-lisp :noweb-ref ORG_CAPTURE
("t" "TODO inbox"
 entry
 (file "~/gtd/capture.org")
 "* TODO %?
    SCHEDULED: %t")
#+end_src

#+begin_src emacs-lisp :noweb-ref ORG_CAPTURE
("n" "notes inbox"
 entry
 (file "~/gtd/inbox.org")
 "* %T\n%i%?")
#+end_src

Result:
#+begin_example
(setq org-capture-templates
      '(
        ("t" "TODO inbox"
         entry
         (file "~/gtd/capture.org")
         "* TODO %?
            SCHEDULED: %t")
        ("n" "notes inbox"
         entry
         (file "~/gtd/inbox.org")
         "* %T\n%i%?")))
#+end_example

Question 2: Is this a bug? If not, how can I produce the expected result using #+NAME naming?
(I prefer #+NAME over :noweb-ref because that line is more obvious and easily to be read since it can be fontified to stand out in different colour for a given Emacs theme.)



* What I Have Done
--------------------
Read the Org-mode info Section 15.10 "Noweb Reference Syntax".



* My Environments
--------------------
OS: Ubuntu 18.04.3 LTS x86_64
Emacs version: GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.30, cairo version 1.15.10) of 2020-10-14
Org-mode version: Org mode version 9.3 (release_9.3 @ /usr/local/share/emacs/28.0.50/lisp/org/)

You can reproduce my "experiment" with the attached Org file. Tangle the attached file and you will get eight files (eight because there are eight different combinations of placing style * naming style). Then tangle each of the eight file to see the result for yourself.



----
Regards,
Jia Hong