8

Interesting in OrgMode version 9+

Is there a way to reference and evaluate code block from other org file and to use it's result in main code block in current org file?

Or maybe just to include code block from other org file to current code block to compose last one as

#+BEGIN_SRS lang :noweb yes
<<reusable-part1>>
<<current-buffer-specific-part>>
<<reusable-part2>>
#+END_SRS

and be possible to C-c C-c it.

2 Answers 2

10

I think that Library of Babel is the best way to reuse code across org-mode files.

For any user to add code to the library, first save the code in regular ‘src’ code blocks of an Org file, and then load the Org file with org-babel-lob-ingest, which is bound to C-c C-v i

To be more explicit, lets say that you have a simple code like this one:

#+NAME: simpleShExample
#+BEGIN_SRC sh :var x="default X" :var y="default Y" :export none
  echo "Var: $x, $y"
#+END_SRC

This code can be reused with:

#+CALL: simpleShExample(y="My y")

which prints:

Var: default X, My y

Now if you save your code in an org file, for instance simpleShExample.org then you can reuse it in any other org files by loading it with this lisp command:

(org-babel-lob-ingest "path/to/simpleShExample.org")

Note that you can do that once in your emacs init file.

You can check the list of all the source code blocks you have ingested by inspecting the org-babel-library-of-babel variable.

5
  • 2
    Well, seems that's not the best, but the ONLY way to reuse code blocks for evaluation :( It's pity that there is no modular way to plugin other files with code blocks
    – Dima Fomin
    Commented Nov 2, 2017 at 10:08
  • I am quite happy with the solution I posted, however maybe that we have not the same requirements. Did you try playing with #+INCLUDE ? orgmode.org/manual/Include-files.html to directly include your code blocks? Commented Nov 2, 2017 at 10:15
  • 1
    Yep. #+INCLUDE is ok for tangling, but not working with live-blocks. Also I cannot make #+SETUPFILE works as well in C-c C-c context.
    – Dima Fomin
    Commented Nov 2, 2017 at 11:27
  • 1
    My idea is to replace Project Jupyter with Org-mode. And to plugin diffent org files with code blocks on-demand as libs
    – Dima Fomin
    Commented Nov 2, 2017 at 11:29
  • 1
    @DimaFomin ok, I understand the motivation :) and I have also ruminated about that. However, my current approach is to doc with org mode but to only provide links to the relevant files: your jupyter notebooks in your case (my C++ code and Mathematica scripts in my cases). The reason why I finally do that is that I realized that If I use org-mode to store code source I lose all the natives tools: completion for C++ code, editor & graphics output for Mathematica... etc. Commented Nov 2, 2017 at 14:34
9

The following works for me: Put this in file one.org:

#+name: the-content
#+begin_src emacs-lisp
(print "hello")
#+end_src

#+name: the-wrapper
#+begin_src org :noweb yes
<<the-content>>
#+end_src

and this in file two.org:

#+begin_src emacs-lisp :noweb yes
<<one.org:the-wrapper()>>
#+end_src

executing this block responses "hello". C-c C-v C-v confirms that the source from the-content ends up in the source block.

This solution utilizes that

  • noweb blocks can call src-blocks
  • org-babel calls can call from other files
  • org src-blocks respond to a call with their content

Therefore org src-blocks can be forwarded over files. I use a wrap-org-block (the-wrapper) that I have the actual language I'm interested in available in the original src-block (the-content).

This solution might cause trouble, when elements of the original src-block are interpreted as org-code in the wrapper-block!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.