PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/testing/examples/babel.org

https://bitbucket.org/Danya/org-mode
Org | 452 lines | 368 code | 83 blank | 1 comment | 1 complexity | f358286ee1e7e03d762b47d5947b6a9c MD5 | raw file
  1. #+Title: a collection of examples for Babel tests
  2. #+OPTIONS: ^:nil
  3. * =:noweb= header argument expansion
  4. :PROPERTIES:
  5. :ID: eb1f6498-5bd9-45e0-9c56-50717053e7b7
  6. :END:
  7. #+name: noweb-example
  8. #+begin_src emacs-lisp :results silent :exports code
  9. (message "expanded1")
  10. #+end_src
  11. #+name: noweb-example2
  12. #+begin_src emacs-lisp :results silent
  13. (message "expanded2")
  14. #+end_src
  15. #+begin_src emacs-lisp :noweb yes :results silent
  16. ;; noweb-1-yes-start
  17. <<noweb-example>>
  18. #+end_src
  19. #+begin_src emacs-lisp :noweb no :results silent
  20. ;; noweb-no-start
  21. <<noweb-example1>>
  22. #+end_src
  23. #+begin_src emacs-lisp :noweb yes :results silent
  24. ;; noweb-2-yes-start
  25. <<noweb-example2>>
  26. #+end_src
  27. #+begin_src emacs-lisp :noweb tangle :results silent
  28. ;; noweb-tangle-start
  29. <<noweb-example1>>
  30. <<noweb-example2>>
  31. #+end_src
  32. * =:noweb= header argument expansion using :exports results
  33. :PROPERTIES:
  34. :ID: 8701beb4-13d9-468c-997a-8e63e8b66f8d
  35. :END:
  36. #+name: noweb-example
  37. #+begin_src emacs-lisp :exports results
  38. (message "expanded1")
  39. #+end_src
  40. #+name: noweb-example2
  41. #+begin_src emacs-lisp :exports results
  42. (message "expanded2")
  43. #+end_src
  44. #+begin_src emacs-lisp :noweb yes :exports results
  45. ;; noweb-1-yes-start
  46. <<noweb-example>>
  47. #+end_src
  48. #+begin_src emacs-lisp :noweb no :exports code
  49. ;; noweb-no-start
  50. <<noweb-example1>>
  51. #+end_src
  52. #+begin_src emacs-lisp :noweb yes :exports results
  53. ;; noweb-2-yes-start
  54. <<noweb-example2>>
  55. #+end_src
  56. #+begin_src emacs-lisp :noweb tangle :exports code
  57. <<noweb-example1>>
  58. <<noweb-example2>>
  59. #+end_src
  60. * excessive id links on tangling
  61. :PROPERTIES:
  62. :ID: ef06fd7f-012b-4fde-87a2-2ae91504ea7e
  63. :END:
  64. ** no, don't give me an ID
  65. #+begin_src emacs-lisp :tangle no
  66. (message "not to be tangled")
  67. #+end_src
  68. ** yes, I'd love an ID
  69. :PROPERTIES:
  70. :ID: ae7b55ca-9ef2-4d30-bd48-da30e35fd0f3
  71. :END:
  72. #+begin_src emacs-lisp :tangle no
  73. (message "for tangling")
  74. #+end_src
  75. * simple named code block
  76. :PROPERTIES:
  77. :ID: 0d82b52d-1bb9-4916-816b-2c67c8108dbb
  78. :END:
  79. #+name: i-have-a-name
  80. #+begin_src emacs-lisp
  81. 42
  82. #+end_src
  83. #+name:
  84. : 42
  85. #+name: i-have-a-name
  86. : 42
  87. * Pascal's Triangle -- exports both test
  88. :PROPERTIES:
  89. :ID: 92518f2a-a46a-4205-a3ab-bcce1008a4bb
  90. :END:
  91. #+name: pascals-triangle
  92. #+begin_src emacs-lisp :var n=5 :exports both
  93. (require 'cl)
  94. (defalias 'my-map (if (org-version-check "24.2.50" "cl" :predicate)
  95. 'cl-map
  96. 'map))
  97. (defun pascals-triangle (n)
  98. (if (= n 0)
  99. (list (list 1))
  100. (let* ((prev-triangle (pascals-triangle (- n 1)))
  101. (prev-row (car (reverse prev-triangle))))
  102. (append prev-triangle
  103. (list (my-map 'list #'+
  104. (append prev-row '(0))
  105. (append '(0) prev-row)))))))
  106. (pascals-triangle n)
  107. #+end_src
  108. * calling code blocks from inside table
  109. :PROPERTIES:
  110. :ID: 6d2ff4ce-4489-4e2a-9c65-e3f71f77d975
  111. :END:
  112. #+name: take-sqrt
  113. #+begin_src emacs-lisp :var n=9
  114. (sqrt n)
  115. #+end_src
  116. * executing an lob call line
  117. :PROPERTIES:
  118. :results: silent
  119. :ID: fab7e291-fde6-45fc-bf6e-a485b8bca2f0
  120. :END:
  121. #+call: echo(input="testing")
  122. #+call: echo(input="testing") :results vector
  123. #+call: echo[:var input="testing"]()
  124. #+call: echo[:var input="testing"]() :results vector
  125. #+call: echo("testing")
  126. #+call: echo("testing") :results vector
  127. This is an inline call call_echo(input="testing") embedded in prose.
  128. This is an inline call call_echo(input="testing")[:results vector] embedded in prose.
  129. #+call: lob-minus(8, 4)
  130. call_echo("testing")
  131. call_concat(1,2,3)
  132. #+name: concat
  133. #+begin_src emacs-lisp :var a=0 :var b=0 :var c=0
  134. (format "%S%S%S" a b c)
  135. #+end_src
  136. * exporting an lob call line
  137. :PROPERTIES:
  138. :ID: 72ddeed3-2d17-4c7f-8192-a575d535d3fc
  139. :END:
  140. #+name: double
  141. #+begin_src emacs-lisp :var it=0
  142. (* 2 it)
  143. #+end_src
  144. The following exports as a normal call line
  145. #+call: double(it=0)
  146. Now here is an inline call call_double(it=1) stuck in the middle of
  147. some prose.
  148. This one should not be exported =call_double(it=2)= because it is
  149. quoted.
  150. Finally this next one should export, even though it starts a line
  151. call_double(it=3) because sometimes inline blocks fold with a
  152. paragraph.
  153. And, a call with raw results call_double(4)[:results raw] should not
  154. have quoted results.
  155. The following 2*5=call_double(5) should export even when prefixed by
  156. an = sign.
  157. * inline source block
  158. :PROPERTIES:
  159. :results: silent
  160. :ID: 54cb8dc3-298c-4883-a933-029b3c9d4b18
  161. :END:
  162. Here is one in the middle src_sh{echo 1} of a line.
  163. Here is one at the end of a line. src_sh{echo 2}
  164. src_sh{echo 3} Here is one at the beginning of a line.
  165. * mixed blocks with exports both
  166. :PROPERTIES:
  167. :ID: 5daa4d03-e3ea-46b7-b093-62c1b7632df3
  168. :END:
  169. #+name: a-list
  170. - a
  171. - b
  172. - c
  173. #+begin_src emacs-lisp :exports both
  174. "code block results"
  175. #+end_src
  176. #+begin_src emacs-lisp :var lst=a-list :results list :exports both
  177. (reverse lst)
  178. #+end_src
  179. * using the =:noweb-ref= header argument
  180. :PROPERTIES:
  181. :ID: 54d68d4b-1544-4745-85ab-4f03b3cbd8a0
  182. :noweb-sep: ""
  183. :END:
  184. #+begin_src sh :tangle yes :noweb yes :shebang "#!/bin/sh"
  185. <<fullest-disk>>
  186. #+end_src
  187. ** query all mounted disks
  188. #+begin_src sh :noweb-ref fullest-disk
  189. df
  190. #+end_src
  191. ** strip the header row
  192. #+begin_src sh :noweb-ref fullest-disk
  193. |sed '1d'
  194. #+end_src
  195. ** sort by the percent full
  196. #+begin_src sh :noweb-ref fullest-disk
  197. |awk '{print $5 " " $6}'|sort -n |tail -1
  198. #+end_src
  199. ** extract the mount point
  200. #+begin_src sh :noweb-ref fullest-disk
  201. |awk '{print $2}'
  202. #+end_src
  203. * resolving sub-trees as references
  204. :PROPERTIES:
  205. :ID: 2409e8ba-7b5f-4678-8888-e48aa02d8cb4
  206. :results: silent
  207. :END:
  208. #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
  209. (length text)
  210. #+end_src
  211. #+begin_src org :noweb yes
  212. <<simple-subtree>>
  213. <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
  214. #+end_src
  215. ** simple subtree with custom ID
  216. :PROPERTIES:
  217. :CUSTOM_ID: simple-subtree
  218. :END:
  219. this is simple
  220. ** simple subtree with global ID
  221. :PROPERTIES:
  222. :ID: d4faa7b3-072b-4dcf-813c-dd7141c633f3
  223. :END:
  224. has length 14
  225. * org-babel-get-inline-src-block-matches
  226. :PROPERTIES:
  227. :results: silent
  228. :ID: 0D0983D4-DE33-400A-8A05-A225A567BC74
  229. :END:
  230. src_sh{echo "One"} block at start of line
  231. One spaced block in src_sh{ echo "middle" } of line
  232. src_sh{echo 2} blocks on the src_emacs-lisp{"same"} line
  233. Inline block with src_sh[:results silent]{ echo "parameters" }.
  234. * exporting a code block with a name
  235. :PROPERTIES:
  236. :ID: b02ddd8a-eeb8-42ab-8664-8a759e6f43d9
  237. :END:
  238. exporting a code block with a name
  239. #+name: qux
  240. #+begin_src sh :foo "baz"
  241. echo bar
  242. #+end_src
  243. * noweb no-export and exports both
  244. :PROPERTIES:
  245. :ID: 8a820f6c-7980-43db-8a24-0710d33729c9
  246. :END:
  247. Weird interaction.
  248. here is one block
  249. #+name: noweb-no-export-and-exports-both-1
  250. #+BEGIN_SRC sh :exports none
  251. echo 1
  252. #+END_SRC
  253. and another
  254. #+BEGIN_SRC sh :noweb no-export :exports both
  255. # I am inside the code block
  256. <<noweb-no-export-and-exports-both-1>>
  257. #+END_SRC
  258. * in order evaluation on export
  259. :PROPERTIES:
  260. :exports: results
  261. :ID: 96cc7073-97ec-4556-87cf-1f9bffafd317
  262. :END:
  263. First.
  264. #+name: foo-for-order-of-evaluation
  265. #+begin_src emacs-lisp :var it=1
  266. (push it *evaluation-collector*)
  267. #+end_src
  268. Second
  269. #+begin_src emacs-lisp
  270. (push 2 *evaluation-collector*)
  271. #+end_src
  272. Third src_emacs-lisp{(push 3 *evaluation-collector*)}
  273. Fourth
  274. #+call: foo-for-order-of-evaluation(4)
  275. Fifth
  276. #+begin_src emacs-lisp
  277. (push 5 *evaluation-collector*)
  278. #+end_src
  279. * exporting more than just results from a call line
  280. :PROPERTIES:
  281. :ID: bec63a04-491e-4caa-97f5-108f3020365c
  282. :END:
  283. Here is a call line with more than just the results exported.
  284. #+call: double(8)
  285. * strip noweb references on export
  286. :PROPERTIES:
  287. :ID: 8e7bd234-99b2-4b14-8cd6-53945e409775
  288. :END:
  289. #+name: strip-export-1
  290. #+BEGIN_SRC sh :exports none
  291. i="10"
  292. #+END_SRC
  293. #+BEGIN_SRC sh :noweb strip-export :exports code :results silent
  294. <<strip-export-1>>
  295. echo "1$i"
  296. #+END_SRC
  297. * use case of reading entry properties
  298. :PROPERTIES:
  299. :ID: cc5fbc20-bca5-437a-a7b8-2b4d7a03f820
  300. :END:
  301. Use case checked and documented with this test: During their
  302. evaluation the source blocks read values from properties from the
  303. entry where the call has been made unless the value is overridden with
  304. the optional argument of the caller.
  305. ** section
  306. :PROPERTIES:
  307. :a: 1
  308. :c: 3
  309. :END:
  310. Note: Just export of a property can be done with a macro: {{{property(a)}}}.
  311. #+NAME: src_block_location_shell sect call
  312. #+CALL: src_block_location_shell()
  313. #+NAME: src_block_location_elisp sect call
  314. #+CALL: src_block_location_elisp()
  315. - sect inline call_src_block_location_shell()
  316. - sect inline call_src_block_location_elisp()
  317. *** subsection
  318. :PROPERTIES:
  319. :b: 2
  320. :c: 4
  321. :END:
  322. #+NAME: src_block_location_shell sub0 call
  323. #+CALL: src_block_location_shell()
  324. #+NAME: src_block_location_elisp sub0 call
  325. #+CALL: src_block_location_elisp()
  326. - sub0 inline call_src_block_location_shell()
  327. - sub0 inline call_src_block_location_elisp()
  328. #+NAME: src_block_location_shell sub1 call
  329. #+CALL: src_block_location_shell(c=5, e=6)
  330. #+NAME: src_block_location_elisp sub1 call
  331. #+CALL: src_block_location_elisp(c=5, e=6)
  332. - sub1 inline call_src_block_location_shell(c=5, e=6)
  333. - sub1 inline call_src_block_location_elisp(c=5, e=6)
  334. **** function definition
  335. #+NAME: src_block_location_shell
  336. #+HEADER: :var a=(or (org-entry-get org-babel-current-src-block-location "a" t) "0")
  337. #+HEADER: :var b=(or (org-entry-get org-babel-current-src-block-location "b" t) "0")
  338. #+HEADER: :var c=(or (org-entry-get org-babel-current-src-block-location "c" t) "0")
  339. #+HEADER: :var d=(or (org-entry-get org-babel-current-src-block-location "d" t) "0")
  340. #+HEADER: :var e=(or (org-entry-get org-babel-current-src-block-location "e" t) "0")
  341. #+BEGIN_SRC sh :shebang #!/bin/sh :exports results :results verbatim
  342. printf "shell a:$a, b:$b, c:$c, d:$d, e:$e"
  343. #+END_SRC
  344. #+RESULTS: src_block_location_shell
  345. #+NAME: src_block_location_elisp
  346. #+HEADER: :var a='nil
  347. #+HEADER: :var b='nil
  348. #+HEADER: :var c='nil
  349. #+HEADER: :var d='nil
  350. #+HEADER: :var e='nil
  351. #+BEGIN_SRC emacs-lisp :exports results
  352. (setq
  353. a (or a (string-to-number
  354. (or (org-entry-get org-babel-current-src-block-location "a" t)
  355. "0")))
  356. b (or b (string-to-number
  357. (or (org-entry-get org-babel-current-src-block-location "b" t)
  358. "0")))
  359. c (or c (string-to-number
  360. (or (org-entry-get org-babel-current-src-block-location "c" t)
  361. "0")))
  362. d (or d (string-to-number
  363. (or (org-entry-get org-babel-current-src-block-location "e" t)
  364. "0")))
  365. e (or e (string-to-number
  366. (or (org-entry-get org-babel-current-src-block-location "d" t)
  367. "0"))))
  368. (format "elisp a:%d, b:%d, c:%d, d:%d, e:%d" a b c d e)
  369. #+END_SRC