9

The source block what I want to evaluate (src2) need to source a file which should be the tangle result of another source block (src1). How could it make the tangle to file automatically everything time I evaluate the source block?

src1: should be tangle to file:

+begin_src tcl :tangle /tmp/compile_lib.tcl
  <some tcl code>
+end_src

src2: would be evaluated.

+begin_src sh
   vivado -mode batch -source /tmp/compile_lib.tcl    # source the file tangled from <src1>
+end_src

2 Answers 2

3

After look around, I work out a workaround to do it by set a dummy variable for the src2 block to tangle the src1 block.

Solution: 1. First, you have to name the src1 block to make it easy be located. 2. Create a dummy variable to use elisp to tangle src1.

Example Code:

#+NAME: my_hello
#+BEGIN_SRC emacs-lisp :tangle /tmp/hello.el
  (message "Hello")
#+END_SRC

#+BEGIN_SRC sh :var DUMMY=(progn (org-babel-goto-named-src-block "my_hello") (org-babel-tangle '(4))) :results output
  cat /tmp/hello.el
  rm -f /tmp/hello.el
#+END_SRC

#+RESULTS:
: (message "Hello")

This solution solve my problem for now. But it has a lot of limitations, such as it can only tangle the block in the same org-mode file.

Hope some would work out a better way to do it.

2

If you need everything to tangle into a single file, you can use this incredibly hacky workaround that uses orgmode to turn a table into generated code in a second (or third!) language.

Your table (1):

#+tblname: personae-table
| Set | Description        | variable | Value |
|-----+--------------------+----------+-------|
| DEP | Fam Med Department | famDep   | 23    |

Your code generator (2):

#+name: python-generate-new-and-set-vars-blocks
#+BEGIN_SRC python :var records=personae-table :exports results :results output verbatim drawer
  print("#+name: variables")
  print("#+BEGIN_SRC mumps")

  print("n " + ",".join(line[3] for line in records))
  print("s " +
        ",".join(
            "{}={}".format([line[2], str(line[3])]) for line in records))
  print()
  print("#+END_SRC")
#+END_SRC

Your generator's results (3):

#+results: python-generate-new-and-set-vars-blocks
:RESULTS:
#+name: variables
#+BEGIN_SRC mumps
n famDep
s famDep=23
#+END_SRC
:END:

Your dummy block that pre-compiles the blocks to tangle (4):

#+BEGIN_SRC emacs-lisp :tangle yes :var DUMMY=(progn (org-babel-goto-named-src-block "python-generate-new-and-set-vars-blocks") (org-ctrl-c-ctrl-c))
; run pre-tangle hooks by putting them in the var.
#+END_SRC

Your output block that tangles the generated code (5):

#+BEGIN_SRC mumps :tangle fixed.m :noweb tangle :padline no
  run()
    <<variables>>
#+END_SRC

Execution goes 4, 1, 2, 3, 5. I'm not sure if I should be proud or ashamed. Orgmode is amazing.

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.