4

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?

Melioratus
  • 4,504
  • 1
  • 25
  • 43
stardiviner
  • 1,888
  • 26
  • 45
  • 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 03: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 03: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 03:29
  • Literate Programming in org-mode has been my preferred coding method I’ve used everyday for about 4 years. Over the years, I learned to use several different writing styles. I adjust the structure and style of the document to increase understanding, improve code quality and lower long term maintenance. If my first answer doesn’t help, I probably have several other well used tools in the LP toolbox that could help. – Melioratus Jun 24 '18 at 03:53
  • Hmm, that's great to hear this. Would you post your answer? I think that could help. You can give out some simple Org example as demo like in my question. WDYT? – stardiviner Jun 25 '18 at 03:48
  • Reading your fun example code and I see the workflow in the source file. Are you planning to call the original code blocks, `<>`? The results,`<>`? Both? For example, Did you want include/mix-and-match code and/or results as steps in other workflows/scripts/files? These questions maybe out of scope, so I'll work on the short answer how to do remote `#+call:` . Thanks – Melioratus Jun 25 '18 at 18:09
  • As I mentioned before, please let me know if my answer(s) will not meet your use case or are unclear. I'm happy to make updates or post new content. Finding and providing useful answers for the community is why I joined emacs stackexchange in the first place. – Melioratus Jul 04 '18 at 20:22
  • Sorry for late response, I recently travel in other place. Busy on other stuff. Your answer is great. The file local variables idea is just awesome in my use case. Hi, and thanks. – stardiviner Jul 14 '18 at 09:18
  • Sorry for late response, I recently travel in other place. Busy on other stuff. Your answer is great. The file local variables idea is just awesome in my use case. Hi, and thanks. – stardiviner Jul 14 '18 at 09:19
  • No worries! Thanks for asking a great question! Thanks for accepting my answer! – Melioratus Jul 14 '18 at 19:24

1 Answers1

4

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

Melioratus
  • 4,504
  • 1
  • 25
  • 43