2

For example, I have following org file:

***** user level

****** copy system service file

At first copy system's systemd service file to systemd user level. by check it
out, and tangle it to target file.

#+begin_src shell :results code :wrap "src systemd :tangle ~/.config/systemd/user/transmission.service" :cache no
systemctl cat transmission.service
#+end_src

#+RESULTS[<2018-03-01 16:25:30> 6f8f1e5900c5be8ab0fa4ee35ff6682e6bc752f0]:
#+begin_src systemd :tangle ~/.config/systemd/user/transmission.service
# /usr/lib/systemd/system/transmission.service
[Unit]
Description=Transmission BitTorrent Daemon
After=network.target

[Service]
User=transmission
Type=notify
ExecStart=/usr/bin/transmission-daemon -f --log-error
ExecReload=/bin/kill -s HUP $MAINPID

[Install]
WantedBy=multi-user.target
#+end_src

Then modify daemon run options:

****** specify daemon config file

#+begin_src shell :results code :wrap src systemd :cache no
gawk -i inplace '/ExecStart/ { gsub(/transmission-daemon/, "transmission-daemon -g ~/.config/transmission") }; { print }' ~/.config/systemd/user/transmission.service
cat ~/.config/systemd/user/transmission.service
#+end_src

#+RESULTS[<2018-03-01 16:31:51> 1b2d0ea649dc85ddc6e96d232d0c969888745a03]:
#+begin_src systemd
# /usr/lib/systemd/system/transmission.service
[Unit]
Description=Transmission BitTorrent Daemon
After=network.target

[Service]
User=transmission
Type=notify
ExecStart=/usr/bin/transmission-daemon -g ~/.config/transmission -f --log-error
ExecReload=/bin/kill -s HUP $MAINPID

[Install]
WantedBy=multi-user.target
#+end_src

I want to call all those src blocks under specific headline user level. Org-mode has a remote code block call syntax like this:

#+call: si.org:figure-1()

But it need to specify one src block. When I have many blocks under headline, I don't want to remote call one by one manually.

So is there a way to do it?

  • I'm not able to get your remote call syntax to work. Would you update your question with the reference to the remote call syntax please? – Melioratus Jun 22 '18 at 22:06
  • Would you add the version of emacs and org-mode that you are using to your question? - Thanks – Melioratus Jun 22 '18 at 22:07
  • That's what I'm asking, the syntax example is just an demo, not really work. So it's not related to Emacs, Org-mode versions. – stardiviner Jun 24 '18 at 3:07
  • Thanks that makes more sense. Do you need to use anonymous src blocks or would it be ok to assign unique names to each src block? If you’re willing to assign unique names then I think I know how to make remote call to that name work. Does that sound close to the answer you would prefer? – Melioratus Jun 24 '18 at 3:23
  • Seems a little close, but anonymous src block or unique name is not what I want. I want significative name for src block for literate programming. But I would like to hear you solution. Check it out whether it is helpful for me. Thanks a lot. – stardiviner Jun 24 '18 at 3:29
1

Ingest Named Code Blocks from other files using org-babel-lob-ingest

  1. Add org-babel-lob-ingest command using File Local Variables syntax to ingest named src blocks from an external file for use in current file.

    • For example, to make the named blocks inside a file named named-blocks-file.org available to another org-mode file:

      1. Add the line below at the top of the other file, e.g. other-file.org.

        # -*- mode: org; org-confirm-babel-evaluate: nil; eval: (org-babel-lob-ingest "./named-blocks-file.org"); -*-
        
      2. Save and close other file.

      3. Open the other file again and then answer y when prompted to apply local variable.

        • If named-blocks-file.org contained the content below:

          * Heading1
          ** Subheading1
             #+NAME: Heading1/Subheading1-Code
             #+BEGIN_SRC shell 
               echo "Code from Heading1/Subheading1"
             #+END_SRC
          ** Subheading2
             #+NAME: Heading1/Subheading2-Code
             #+BEGIN_SRC shell 
               echo "Code from Heading1/Subheading2"
             #+END_SRC
          
        • You should see a message in the mini-buffer similar to the example below:

          2 src blocks added to Library of Babel
          
  2. Evalute code blocks inside other file using following built-in methods:

    • Use #+CALL: Evaluation Remote Code Blocks

      ** Call =Heading1/Subheading1-Code=
         #+CALL: Heading1/Subheading1-Code()
      ** Call =Heading1/Subheading2-Code=
         #+CALL: Heading1/Subheading2-Code()
      
    • Use :noweb header to embed code or code results into code blocks

      • Embedding Code into Code Block

        #+BEGIN_SRC shell :noweb yes :shebang "#!/usr/bin/env bash"
           echo "Calling Embedded Remote Code"
           <<Heading1/Subheading1-Code>>
           <<Heading1/Subheading2-Code>>
        
        #+END_SRC
        
        • Is transformed into this script

          #+BEGIN_EXAMPLE shell
            #!/usr/bin/env bash
            echo "Calling Embedded Remote Code"
            echo "Code from Heading1/Subheading1"
            echo "Code from Heading1/Subheading2"
          
          #+END_EXAMPLE
          
    • Embedding Code Results

      • Embedding Code Results into Code Block

        #+BEGIN_SRC shell :noweb yes :shebang "#!/usr/bin/env bash"
          echo "Calling Embedded Remote Code Results"
          echo "Results: '<<Heading1/Subheading1-Code()>>'"
          echo "Results: '<<Heading1/Subheading2-Code()>>'"
        
        #+END_SRC
        
      • Is transformed into this script

        #+BEGIN_EXAMPLE shell
          #!/usr/bin/env bash
          echo "Calling Embedded Remote Code Results"
          echo "Results: 'Code from Heading1/Subheading1'"
          echo "Results: 'Code from Heading1/Subheading2'"
        
        #+END_EXAMPLE
        

This code was tested using:
emacs version: GNU Emacs 25.2.1 (x86_64-unknown-cygwin, GTK+ Version 3.22.10)
org-mode version: 9.1.2

Your Answer

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

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