1 #+Title: a collection of examples for Babel tests
4 * =:noweb= header argument expansion
6 :ID: eb1f6498-5bd9-45e0-9c56-50717053e7b7
10 #+begin_src emacs-lisp :results silent :exports code
14 #+name: noweb-example2
15 #+begin_src emacs-lisp :results silent
19 #+begin_src emacs-lisp :noweb yes :results silent
24 #+begin_src emacs-lisp :noweb no :results silent
29 #+begin_src emacs-lisp :noweb yes :results silent
34 #+begin_src emacs-lisp :noweb tangle :results silent
40 * =:noweb= header argument expansion using :exports results
42 :ID: 8701beb4-13d9-468c-997a-8e63e8b66f8d
46 #+begin_src emacs-lisp :exports results
50 #+name: noweb-example2
51 #+begin_src emacs-lisp :exports results
55 #+begin_src emacs-lisp :noweb yes :exports results
60 #+begin_src emacs-lisp :noweb no :exports code
65 #+begin_src emacs-lisp :noweb yes :exports results
70 #+begin_src emacs-lisp :noweb tangle :exports code
75 * excessive id links on tangling
77 :ID: ef06fd7f-012b-4fde-87a2-2ae91504ea7e
80 ** no, don't give me an ID
81 #+begin_src emacs-lisp :tangle no
82 (message "not to be tangled")
85 ** yes, I'd love an ID
87 :ID: ae7b55ca-9ef2-4d30-bd48-da30e35fd0f3
89 #+begin_src emacs-lisp :tangle no
90 (message "for tangling")
92 * simple named code block
94 :ID: 0d82b52d-1bb9-4916-816b-2c67c8108dbb
98 #+begin_src emacs-lisp
105 #+name: i-have-a-name
108 * Pascal's Triangle -- exports both test
110 :ID: 92518f2a-a46a-4205-a3ab-bcce1008a4bb
113 #+name: pascals-triangle
114 #+begin_src emacs-lisp :var n=5 :exports both
116 (defalias 'my-map (if (org-version-check "24.2.50" "cl" :predicate)
119 (defun pascals-triangle (n)
122 (let* ((prev-triangle (pascals-triangle (- n 1)))
123 (prev-row (car (reverse prev-triangle))))
124 (append prev-triangle
125 (list (my-map 'list #'+
126 (append prev-row '(0))
127 (append '(0) prev-row)))))))
132 * calling code blocks from inside table
134 :ID: 6d2ff4ce-4489-4e2a-9c65-e3f71f77d975
138 #+begin_src emacs-lisp :var n=9
142 * executing an lob call line
145 :ID: fab7e291-fde6-45fc-bf6e-a485b8bca2f0
148 #+call: echo(input="testing")
149 #+call: echo(input="testing") :results vector
150 #+call: echo[:var input="testing"]()
151 #+call: echo[:var input="testing"]() :results vector
152 #+call: echo("testing")
153 #+call: echo("testing") :results vector
154 This is an inline call call_echo(input="testing") embedded in prose.
155 This is an inline call call_echo(input="testing")[:results vector] embedded in prose.
156 #+call: lob-minus(8, 4)
161 #+begin_src emacs-lisp :var a=0 :var b=0 :var c=0
162 (format "%S%S%S" a b c)
165 * exporting an lob call line
167 :ID: 72ddeed3-2d17-4c7f-8192-a575d535d3fc
171 #+begin_src emacs-lisp :var it=0
175 The following exports as a normal call line
178 Now here is an inline call call_double(it=1) stuck in the middle of
181 This one should not be exported =call_double(it=2)= because it is
184 Finally this next one should export, even though it starts a line
185 call_double(it=3) because sometimes inline blocks fold with a
188 And, a call with raw results call_double(4)[:results raw] should not
191 The following 2*5=call_double(5) should export even when prefixed by
194 * inline source block
196 :ID: 54cb8dc3-298c-4883-a933-029b3c9d4b18
198 Here is one in the middle src_sh{echo 1} of a line.
199 Here is one at the end of a line. src_sh{echo 2}
200 src_sh{echo 3} Here is one at the beginning of a line.
202 * exported inline source block
204 :ID: cd54fc88-1b6b-45b6-8511-4d8fa7fc8076
207 Here is one in the middle src_sh{echo 1} of a line.
208 Here is one at the end of a line. src_sh{echo 2}
209 src_sh{echo 3} Here is one at the beginning of a line.
210 Here is one that is also evaluated: src_sh[:exports both]{echo 4}
212 * mixed blocks with exports both
214 :ID: 5daa4d03-e3ea-46b7-b093-62c1b7632df3
222 #+begin_src emacs-lisp :exports both
226 #+begin_src emacs-lisp :var lst=a-list :results list :exports both
230 * using the =:noweb-ref= header argument
232 :ID: 54d68d4b-1544-4745-85ab-4f03b3cbd8a0
236 #+begin_src sh :tangle yes :noweb yes :shebang "#!/bin/sh"
240 ** query all mounted disks
241 #+begin_src sh :noweb-ref fullest-disk
245 ** strip the header row
246 #+begin_src sh :noweb-ref fullest-disk
250 ** sort by the percent full
251 #+begin_src sh :noweb-ref fullest-disk
252 |awk '{print $5 " " $6}'|sort -n |tail -1
255 ** extract the mount point
256 #+begin_src sh :noweb-ref fullest-disk
259 * resolving sub-trees as references
261 :ID: 2409e8ba-7b5f-4678-8888-e48aa02d8cb4
265 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
269 #+begin_src org :noweb yes
271 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
274 ** simple subtree with custom ID
276 :CUSTOM_ID: simple-subtree
280 ** simple subtree with global ID
282 :ID: d4faa7b3-072b-4dcf-813c-dd7141c633f3
286 * org-babel-get-inline-src-block-matches
289 :ID: 0D0983D4-DE33-400A-8A05-A225A567BC74
291 src_sh{echo "One"} block at start of line
292 One spaced block in src_sh{ echo "middle" } of line
293 src_sh{echo 2} blocks on the src_emacs-lisp{"same"} line
294 Inline block with src_sh[:results silent]{ echo "parameters" }.
296 * org-babel-get-inline-src-block-matches (with empty args)
299 :ID: d55dada7-de0e-4340-8061-787cccbedee5
301 src_sh[]{echo "One"} block at start of line
302 One spaced block in src_sh[]{ echo "middle" } of line
303 src_sh[]{echo 2} blocks on the src_emacs-lisp[]{"same"} line
304 Inline block with src_sh[:results silent]{ echo "parameters" }.
306 * exporting a code block with a name
308 :ID: b02ddd8a-eeb8-42ab-8664-8a759e6f43d9
311 exporting a code block with a name
313 #+begin_src sh :foo "baz"
316 * noweb no-export and exports both
318 :ID: 8a820f6c-7980-43db-8a24-0710d33729c9
324 #+name: noweb-no-export-and-exports-both-1
325 #+BEGIN_SRC sh :exports none
331 #+BEGIN_SRC sh :noweb no-export :exports both
332 # I am inside the code block
333 <<noweb-no-export-and-exports-both-1>>
336 * in order evaluation on export
339 :ID: 96cc7073-97ec-4556-87cf-1f9bffafd317
343 #+name: foo-for-order-of-evaluation
344 #+begin_src emacs-lisp :var it=1
345 (push it *evaluation-collector*)
349 #+begin_src emacs-lisp
350 (push 2 *evaluation-collector*)
353 Third src_emacs-lisp{(car (push 3 *evaluation-collector*))}
356 #+call: foo-for-order-of-evaluation(4)
359 #+begin_src emacs-lisp
360 (push 5 *evaluation-collector*)
362 * exporting more than just results from a call line
364 :ID: bec63a04-491e-4caa-97f5-108f3020365c
366 Here is a call line with more than just the results exported.
368 * strip noweb references on export
370 :ID: 8e7bd234-99b2-4b14-8cd6-53945e409775
373 #+name: strip-export-1
374 #+BEGIN_SRC sh :exports none
378 #+BEGIN_SRC sh :noweb strip-export :exports code :results silent
383 * use case of reading entry properties
385 :ID: cc5fbc20-bca5-437a-a7b8-2b4d7a03f820
388 Use case checked and documented with this test: During their
389 evaluation the source blocks read values from properties from the
390 entry where the call has been made unless the value is overridden with
391 the optional argument of the caller.
399 Note: Just export of a property can be done with a macro: {{{property(a)}}}.
401 #+NAME: src_block_location_shell-sect-call
402 #+CALL: src_block_location_shell()
404 #+NAME: src_block_location_elisp-sect-call
405 #+CALL: src_block_location_elisp()
407 - sect inline call_src_block_location_shell()[:results raw]
408 - sect inline call_src_block_location_elisp()[:results raw]
416 #+NAME: src_block_location_shell-sub0-call
417 #+CALL: src_block_location_shell()
419 #+NAME: src_block_location_elisp-sub0-call
420 #+CALL: src_block_location_elisp()
422 - sub0 inline call_src_block_location_shell()[:results raw]
423 - sub0 inline call_src_block_location_elisp()[:results raw]
425 #+NAME: src_block_location_shell-sub1-call
426 #+CALL: src_block_location_shell(c=5, e=6)
428 #+NAME: src_block_location_elisp-sub1-call
429 #+CALL: src_block_location_elisp(c=5, e=6)
431 - sub1 inline call_src_block_location_shell(c=5, e=6)[:results raw]
432 - sub1 inline call_src_block_location_elisp(c=5, e=6)[:results raw]
434 **** function definition
437 - The "or" is to deal with a property not present.
438 - The t is to get property inheritance.
439 #+NAME: src_block_location_shell
440 #+HEADER: :var a=(or (org-entry-get org-babel-current-src-block-location "a" t) "0")
441 #+HEADER: :var b=(or (org-entry-get org-babel-current-src-block-location "b" t) "0")
442 #+HEADER: :var c=(or (org-entry-get org-babel-current-src-block-location "c" t) "0")
443 #+HEADER: :var d=(or (org-entry-get org-babel-current-src-block-location "d" t) "0")
444 #+HEADER: :var e=(or (org-entry-get org-babel-current-src-block-location "e" t) "0")
445 #+BEGIN_SRC sh :shebang #!/bin/sh :exports results :results verbatim
446 printf "shell a:$a, b:$b, c:$c, d:$d, e:$e"
449 #+RESULTS: src_block_location_shell
451 #+NAME: src_block_location_elisp
452 #+HEADER: :var a='nil
453 #+HEADER: :var b='nil
454 #+HEADER: :var c='nil
455 #+HEADER: :var d='nil
456 #+HEADER: :var e='nil
457 #+BEGIN_SRC emacs-lisp :exports results
459 ;; - The first `or' together with ":var <var>='nil" is to check for
460 ;; a value bound from an optional call argument, in the examples
462 ;; - The second `or' is to deal with a property not present
463 ;; - The t is to get property inheritance
464 a (or a (string-to-number
465 (or (org-entry-get org-babel-current-src-block-location "a" t)
467 b (or b (string-to-number
468 (or (org-entry-get org-babel-current-src-block-location "b" t)
470 c (or c (string-to-number
471 (or (org-entry-get org-babel-current-src-block-location "c" t)
473 d (or d (string-to-number
474 (or (org-entry-get org-babel-current-src-block-location "e" t)
476 e (or e (string-to-number
477 (or (org-entry-get org-babel-current-src-block-location "d" t)
479 (format "elisp a:%d, b:%d, c:%d, d:%d, e:%d" a b c d e)
482 * =:file-ext= and =:output-dir= header args
484 :ID: 93573e1d-6486-442e-b6d0-3fedbdc37c9b
486 #+name: file-ext-basic
487 #+BEGIN_SRC emacs-lisp :file-ext txt
491 #+name: file-ext-dir-relative
492 #+BEGIN_SRC emacs-lisp :file-ext txt :output-dir foo
496 #+name: file-ext-dir-relative-slash
497 #+BEGIN_SRC emacs-lisp :file-ext txt :output-dir foo/
501 #+name: file-ext-dir-absolute
502 #+BEGIN_SRC emacs-lisp :file-ext txt :output-dir /tmp
506 #+name: file-ext-file-wins
507 #+BEGIN_SRC emacs-lisp :file-ext txt :file foo.bar
511 #+name: output-dir-and-file
512 #+BEGIN_SRC emacs-lisp :output-dir xxx :file foo.bar