This post asks about how you can share static key pair values across different tangled source files. The following are my two tries.
These examples
- Use Emacs Lisp because it is easier for me
- Use the configuration at the bottom
- Define a collection to store the key pair values but you could use anything anywhere you wanted
- At the bottom I included the expanded source blocks for each approach and they are identical in what they do but different in their generated code
Using Variables
Use var
statements. This has the benefit of source blocks that look like “real code”.
#+BEGIN_SRC emacs-lisp :results none (setq myvars (make-hash-table :test 'equal)) (puthash "a" "17" myvars) (puthash "b" "84" myvars) (puthash "default" "no match found" myvars) #+END_SRC #+BEGIN_SRC sh :tangle test1.sh :var a=(gethash "a" myvars) :tangle test.sh echo a #+END_SRC #+BEGIN_SRC emacs-lisp :var b=(gethash "b" myvars) :tangle test1.el (message b) #+END_SRC
Using Noweb Calls
Use noweb calls. This has the benefit of super flexibility (noweb ref calls are just plain old function calls really). It looks more dramatic. The calls really stand out unlike the var
approach.
#+BEGIN_SRC emacs-lisp :results none (setq myvars (make-hash-table :test 'equal)) (puthash "a" "17" myvars) (puthash "b" "84" myvars) (puthash "default" "no match found" myvars) #+END_SRC #+BEGIN_SRC emacs-lisp :results value scalar :noweb-ref myvar :var key="default" (gethash key myvars) #+END_SRC #+BEGIN_SRC sh :tangle test2.sh echo <<myx(key="a")>> #+END_SRC #+BEGIN_SRC emacs-lisp :tangle test2.el (message <<myx(key="b")>>) #+END_SRC
Expanded Source Blocks
- Call
org-babel-expand-src-block
to see how the source block expands. This is a lot faster than tangling the source to see the resulting code or evaluating the source block to see if it works right. - Expansion of the source blocks: they are identical in operation
- Using variables
-
test1.sh
a='17' echo a
-
test1.el
(let ((b (quote "84"))) (message b) )
-
- Using Noweb
-
test2.sh
echo "17"
-
test2.el
(message "84")
-
- Using variables
My Configuration
System
Org-Version: 9.0.9 Org-Git-Version:release_9.0.9-737-g7e46af Emacs-Version: GNU Emacs 25.2.1 (x86_64-apple-darwin15.6.0, Carbon Version 157 AppKit 1404.47) of 2017-07-28 Noweb wrap start and stop delimeters: ’<<’ and ’>>’ org-babel-default-header-args: ((:session . "none") (:results . "replace") (:exports . "code") (:cache . "no") (:noweb . "no") (:hlines . "no") (:tangle . "no"))
Block
Name: org_gcr_2017-07-14_mara_13AA581B-CF83-4044-AE96-B94DEA65FC8C Lang: org Properties: :header-args nil :header-args:org nil Header Arguments: :cache no :comments noweb :eval never-export :exports code :hlines no :mkdirp yes :noweb no-export :padline yes :results silent raw value table :session none :tangle no :wrap EXAMPLE