0

The context

Lately, I've been using the =:noweb= feature which has been of great help. Consider this minimal working example

* Using =std::cout=

#+NAME: a
#+begin_src cpp
std::cout << "a";
#+end_src

#+NAME: b
#+begin_src cpp
std::cout << "b";
#+end_src

#+begin_src cpp :noweb yes
#include <iostream>

int main() {
  <<a>>
  return 0;
}
#+end_src

#+RESULTS:
: a

The problem

Now, let's suppose I create a new section within the same file with the following content.

* The =string= data type

#+NAME: a
#+begin_src cpp
std::string c = "AAA"
#+end_src

#+NAME: b
#+begin_src cpp
std::string c = "BBB"
#+end_src

#+begin_src cpp :noweb yes
#include <iostream>

int main() {
  <<b>>
  std::cout << c;
  return 0;
}
#+end_src

As you can see above, I've used the a and b names again as arguments to the #+NAME tag for the source code blocks present in this heading.

When evaluating the last source code block (the one from the The =string= data type section that contains <<b>>), it seems that org searches the source code block with name b and stop when one has been found. This causes a compilation error 'c' was not declared in this scope because it is selecting the one from the Using =std::cout section. However, I would like org to select the one from the same heading.

Of course, I can create different names, but the problem is that the more source code blocks my file has, the harder it will be to select a name. I could use cout-a and string-a but then I won't be able to use those names again, so whenever I want to name a code block, I would need to look at all the names and think in a new one.

The question

So, my question is: When having source code blocks with the same +#NAME tag, is it possible to make org-mode give higher priority to the ones from the same heading?

2
  • I don't think so. – NickD Sep 13 '20 at 20:14
  • 2
    In fact, org-lint complains with Duplicate NAME if you try to do that. – NickD Sep 14 '20 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.