org-babel.el 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  1. ;;; org-babel.el --- facilitating communication between programming languages and people
  2. ;; Copyright (C) 2009 Eric Schulte, Dan Davison
  3. ;; Author: Eric Schulte, Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; See org-babel.org in the parent directory for more information
  24. ;;; Code:
  25. (require 'org)
  26. (defun org-babel-execute-src-block-maybe ()
  27. "Detect if this is context for a org-babel src-block and if so
  28. then run `org-babel-execute-src-block'."
  29. (interactive)
  30. (let ((info (org-babel-get-src-block-info)))
  31. (if info
  32. (progn (org-babel-execute-src-block current-prefix-arg info) t) nil)))
  33. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-src-block-maybe)
  34. (defun org-babel-expand-src-block-maybe ()
  35. "Detect if this is context for a org-babel src-block and if so
  36. then run `org-babel-expand-src-block'."
  37. (interactive)
  38. (let ((info (org-babel-get-src-block-info)))
  39. (if info
  40. (progn (org-babel-expand-src-block current-prefix-arg info) t)
  41. nil)))
  42. (defadvice org-edit-special (around org-babel-prep-session-for-edit activate)
  43. "Prepare the current source block's session according to it's
  44. header arguments before editing in an org-src buffer. This
  45. function is called when `org-edit-special' is called with a
  46. prefix argument from inside of a source-code block."
  47. (when current-prefix-arg
  48. (let* ((info (org-babel-get-src-block-info))
  49. (lang (first info))
  50. (params (third info))
  51. (session (cdr (assoc :session params))))
  52. (when (and info session) ;; we are in a source-code block with a session
  53. (funcall
  54. (intern (concat "org-babel-prep-session:" lang)) session params))))
  55. ad-do-it)
  56. (defadvice org-open-at-point (around org-babel-open-at-point activate)
  57. "If `point' is on a source code block, then open that block's
  58. results with `org-babel-open-src-block-results', otherwise defer
  59. to `org-open-at-point'."
  60. (interactive "P")
  61. (or (call-interactively #'org-babel-open-src-block-result) ad-do-it))
  62. (defun org-babel-load-in-session-maybe ()
  63. "Detect if this is context for a org-babel src-block and if so
  64. then run `org-babel-load-in-session'."
  65. (interactive)
  66. (let ((info (org-babel-get-src-block-info)))
  67. (if info
  68. (progn (org-babel-load-in-session current-prefix-arg info) t)
  69. nil)))
  70. (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
  71. (defun org-babel-pop-to-session-maybe ()
  72. "Detect if this is context for a org-babel src-block and if so
  73. then run `org-babel-pop-to-session'."
  74. (interactive)
  75. (let ((info (org-babel-get-src-block-info)))
  76. (if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil)))
  77. (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
  78. (defconst org-babel-header-arg-names
  79. '(cache cmdline colnames dir exports file noweb results
  80. session tangle var noeval)
  81. "Common header arguments used by org-babel. Note that
  82. individual languages may define their own language specific
  83. header arguments as well.")
  84. (defvar org-babel-default-header-args
  85. '((:session . "none") (:results . "replace") (:exports . "code")
  86. (:cache . "no") (:noweb . "no") (:hlines . "no"))
  87. "Default arguments to use when evaluating a source block.")
  88. (defvar org-babel-default-inline-header-args
  89. '((:session . "none") (:results . "silent") (:exports . "results"))
  90. "Default arguments to use when evaluating an inline source block.")
  91. (defvar org-babel-src-block-regexp nil
  92. "Regexp used to test when inside of a org-babel src-block")
  93. (defvar org-babel-inline-src-block-regexp nil
  94. "Regexp used to test when on an inline org-babel src-block")
  95. (defvar org-babel-result-regexp
  96. "^[ \t]*#\\+res\\(ults\\|name\\)\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:"
  97. "Regular expression used to match result lines. If the
  98. results are associated with a hash key then the hash will be
  99. saved in the second match data.")
  100. (defvar org-babel-source-name-regexp
  101. "^[ \t]*#\\+\\(srcname\\|source\\|function\\):[ \t]*"
  102. "Regular expression used to match a source name line.")
  103. (defvar org-babel-min-lines-for-block-output 10
  104. "If number of lines of output is equal to or exceeds this
  105. value, the output is placed in a #+begin_example...#+end_example
  106. block. Otherwise the output is marked as literal by inserting
  107. colons at the starts of the lines. This variable only takes
  108. effect if the :results output option is in effect.")
  109. (defvar org-babel-noweb-error-langs nil
  110. "List of language for which errors should be raised when the
  111. source code block satisfying a noweb reference in this language
  112. can not be resolved.")
  113. (defvar org-babel-hash-show 4
  114. "Number of initial characters to show of a hidden results hash.")
  115. (defvar org-babel-after-execute-hook nil
  116. "Hook for functions to be called after `org-babel-execute-src-block'")
  117. (defun org-babel-named-src-block-regexp-for-name (name)
  118. "Regexp used to match named src block."
  119. (concat org-babel-source-name-regexp (regexp-quote name) "[ \t\n]*"
  120. (substring org-babel-src-block-regexp 1)))
  121. (defun org-babel-set-interpreters (var value)
  122. (set-default var value)
  123. (setq
  124. org-babel-src-block-regexp
  125. (concat "^\\([ \t]*\\)#\\+begin_src[ \t]+\\(" ;; (1) indentation (2) lang
  126. (mapconcat 'regexp-quote value "\\|")
  127. "\\)[ \t]*"
  128. "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)" ;; (3) switches
  129. "\\([^\n]*\\)\n" ;; (4) header arguments
  130. "\\([^\000]+?\n\\)[ \t]*#\\+end_src"));; (5) body
  131. (setq org-babel-inline-src-block-regexp
  132. (concat "[ \f\t\n\r\v]\\(src_" ;; (1) replacement target
  133. "\\(" ;; (2) lang
  134. (mapconcat 'regexp-quote value "\\|")
  135. "\\)"
  136. "\\(\\|\\[\\(.*\\)\\]\\)" ;; (3,4) (unused, headers)
  137. "{\\([^\f\n\r\v]+\\)}" ;; (5) body
  138. "\\)")))
  139. (defun org-babel-add-interpreter (interpreter)
  140. "Add INTERPRETER to `org-babel-interpreters' and update
  141. `org-babel-src-block-regexp' appropriately."
  142. (unless (member interpreter org-babel-interpreters)
  143. (setq org-babel-interpreters
  144. (sort (cons interpreter org-babel-interpreters)
  145. (lambda (left right)
  146. (> (length left) (length right)))))
  147. (org-babel-set-interpreters 'org-babel-interpreters org-babel-interpreters)))
  148. (defcustom org-babel-interpreters '()
  149. "Interpreters allows for evaluation tags.
  150. This is a list of program names (as strings) that can evaluate code and
  151. insert the output into an Org-mode buffer. Valid choices are
  152. R Evaluate R code
  153. emacs-lisp Evaluate Emacs Lisp code and display the result
  154. sh Pass command to the shell and display the result
  155. perl The perl interpreter
  156. python The python interpreter
  157. ruby The ruby interpreter
  158. The source block regexp `org-babel-src-block-regexp' is updated
  159. when a new interpreter is added to this list through the
  160. customize interface. To add interpreters to this variable from
  161. lisp code use the `org-babel-add-interpreter' function."
  162. :group 'org-babel
  163. :set 'org-babel-set-interpreters
  164. :type '(set :greedy t
  165. (const "R")
  166. (const "emacs-lisp")
  167. (const "sh")
  168. (const "perl")
  169. (const "python")
  170. (const "ruby")))
  171. ;;; functions
  172. (defun org-babel-execute-src-block (&optional arg info params)
  173. "Execute the current source code block, and insert the results
  174. into the buffer. Source code execution and the collection and
  175. formatting of results can be controlled through a variety of
  176. header arguments.
  177. Optionally supply a value for INFO in the form returned by
  178. `org-babel-get-src-block-info'.
  179. Optionally supply a value for PARAMS which will be merged with
  180. the header arguments specified at the front of the source code
  181. block."
  182. (interactive)
  183. (let* ((info (or info (org-babel-get-src-block-info)))
  184. (lang (first info))
  185. (params (setf (third info)
  186. (sort (org-babel-merge-params (third info) params)
  187. (lambda (el1 el2) (string< (symbol-name (car el1))
  188. (symbol-name (car el2)))))))
  189. (new-hash
  190. (if (and (cdr (assoc :cache params))
  191. (string= "yes" (cdr (assoc :cache params))))
  192. (org-babel-sha1-hash info)))
  193. (old-hash (org-babel-result-hash info))
  194. (body (setf (second info)
  195. (if (and (cdr (assoc :noweb params))
  196. (string= "yes" (cdr (assoc :noweb params))))
  197. (org-babel-expand-noweb-references info)
  198. (second info))))
  199. (result-params (split-string (or (cdr (assoc :results params)) "")))
  200. (result-type (cond ((member "output" result-params) 'output)
  201. ((member "value" result-params) 'value)
  202. (t 'value)))
  203. (cmd (intern (concat "org-babel-execute:" lang)))
  204. (dir (cdr (assoc :dir params)))
  205. (default-directory
  206. (or (and dir (file-name-as-directory dir)) default-directory))
  207. (call-process-region-original
  208. (if (boundp 'call-process-region-original) call-process-region-original
  209. (symbol-function 'call-process-region)))
  210. (indent (car (last info)))
  211. result)
  212. (unwind-protect
  213. (flet ((call-process-region (&rest args)
  214. (apply 'org-babel-tramp-handle-call-process-region args)))
  215. (unless (member lang org-babel-interpreters)
  216. (error "Language is not in `org-babel-interpreters': %s" lang))
  217. (if (and (not arg) new-hash (equal new-hash old-hash))
  218. (save-excursion ;; return cached result
  219. (goto-char (org-babel-where-is-src-block-result nil info))
  220. (end-of-line 1) (forward-char 1)
  221. (setq result (org-babel-read-result))
  222. (message (replace-regexp-in-string "%" "%%"
  223. (format "%S" result))) result)
  224. (setq result (funcall cmd body params))
  225. (if (eq result-type 'value)
  226. (setq result (if (and (or (member "vector" result-params)
  227. (member "table" result-params))
  228. (not (listp result)))
  229. (list (list result))
  230. result)))
  231. (org-babel-insert-result result result-params info new-hash indent)
  232. (run-hooks 'org-babel-after-execute-hook)
  233. result))
  234. (setq call-process-region 'call-process-region-original))))
  235. (defun org-babel-expand-body:generic (body params &optional processed-params)
  236. "Expand a block of code with org-babel according to it's header
  237. arguments. This generic implementation of body expansion is
  238. called for languages which have not defined their own specific
  239. org-babel-expand-body:lang function." body)
  240. (defun org-babel-expand-src-block (&optional arg info params)
  241. "Expand the current source code block according to it's header
  242. arguments, and pop open the results in a preview buffer."
  243. (interactive)
  244. (let* ((info (or info (org-babel-get-src-block-info)))
  245. (lang (first info))
  246. (params (setf (third info)
  247. (sort (org-babel-merge-params (third info) params)
  248. (lambda (el1 el2) (string< (symbol-name (car el1))
  249. (symbol-name (car el2)))))))
  250. (body (setf (second info)
  251. (if (and (cdr (assoc :noweb params))
  252. (string= "yes" (cdr (assoc :noweb params))))
  253. (org-babel-expand-noweb-references info) (second info))))
  254. (cmd (intern (concat "org-babel-expand-body:" lang)))
  255. (expanded (funcall (if (fboundp cmd) cmd 'org-babel-expand-body:generic)
  256. body params)))
  257. (org-edit-src-code
  258. nil expanded (concat "*Org-Babel Preview " (buffer-name) "[ " lang " ]*"))))
  259. (defun org-babel-load-in-session (&optional arg info)
  260. "Load the body of the current source-code block. Evaluate the
  261. header arguments for the source block before entering the
  262. session. After loading the body this pops open the session."
  263. (interactive)
  264. (let* ((info (or info (org-babel-get-src-block-info)))
  265. (lang (first info))
  266. (body (second info))
  267. (params (third info))
  268. (session (cdr (assoc :session params))))
  269. (unless (member lang org-babel-interpreters)
  270. (error "Language is not in `org-babel-interpreters': %s" lang))
  271. ;; if called with a prefix argument, then process header arguments
  272. (pop-to-buffer
  273. (funcall (intern (concat "org-babel-load-session:" lang))
  274. session body params))
  275. (end-of-line 1)))
  276. (defun org-babel-switch-to-session (&optional arg info)
  277. "Switch to the session of the current source-code block.
  278. If called with a prefix argument then evaluate the header arguments
  279. for the source block before entering the session. Copy the body
  280. of the source block to the kill ring."
  281. (interactive)
  282. (let* ((info (or info (org-babel-get-src-block-info)))
  283. (lang (first info))
  284. (body (second info))
  285. (params (third info))
  286. (session (cdr (assoc :session params)))
  287. (dir (cdr (assoc :dir params)))
  288. (default-directory
  289. (or (and dir (file-name-as-directory dir)) default-directory)))
  290. (unless (member lang org-babel-interpreters)
  291. (error "Language is not in `org-babel-interpreters': %s" lang))
  292. ;; copy body to the kill ring
  293. (with-temp-buffer (insert (org-babel-trim body))
  294. (copy-region-as-kill (point-min) (point-max)))
  295. ;; if called with a prefix argument, then process header arguments
  296. (when arg
  297. (funcall (intern (concat "org-babel-prep-session:" lang)) session params))
  298. ;; just to the session using pop-to-buffer
  299. (pop-to-buffer
  300. (funcall (intern (format "org-babel-%s-initiate-session" lang))
  301. session params))
  302. (end-of-line 1)))
  303. (defalias 'org-babel-pop-to-session 'org-babel-switch-to-session)
  304. (defun org-babel-open-src-block-result (&optional re-run)
  305. "If `point' is on a src block then open the results of the
  306. source code block, otherwise return nil. With optional prefix
  307. argument RE-RUN the source-code block is evaluated even if
  308. results already exist."
  309. (interactive "P")
  310. (when (org-babel-get-src-block-info)
  311. (save-excursion
  312. ;; go to the results, if there aren't any then run the block
  313. (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
  314. (progn (org-babel-execute-src-block)
  315. (org-babel-where-is-src-block-result))))
  316. (end-of-line 1)
  317. (while (looking-at "[\n\r\t\f ]") (forward-char 1))
  318. ;; open the results
  319. (if (looking-at org-bracket-link-regexp)
  320. ;; file results
  321. (org-open-at-point)
  322. (let ((results (org-babel-read-result)))
  323. (flet ((echo-res (result)
  324. (if (stringp result) result (format "%S" result))))
  325. (pop-to-buffer (get-buffer-create "org-babel-results"))
  326. (delete-region (point-min) (point-max))
  327. (if (listp results)
  328. ;; table result
  329. (insert (orgtbl-to-generic results '(:sep "\t" :fmt echo-res)))
  330. ;; scalar result
  331. (insert (echo-res results))))))
  332. t)))
  333. (defun org-babel-execute-buffer (&optional arg)
  334. "Call `org-babel-execute-src-block' on every source block in
  335. the current buffer."
  336. (interactive "P")
  337. (save-excursion
  338. (org-save-outline-visibility t
  339. (goto-char (point-min))
  340. (show-all)
  341. (while (re-search-forward org-babel-src-block-regexp nil t)
  342. (let ((pos-end (match-end 0)))
  343. (goto-char (match-beginning 0))
  344. (org-babel-execute-src-block arg)
  345. (goto-char pos-end))))))
  346. (defun org-babel-execute-subtree (&optional arg)
  347. "Call `org-babel-execute-src-block' on every source block in
  348. the current subtree."
  349. (interactive "P")
  350. (save-restriction
  351. (save-excursion
  352. (org-narrow-to-subtree)
  353. (org-babel-execute-buffer)
  354. (widen))))
  355. (defun org-babel-get-src-block-info (&optional header-vars-only)
  356. "Get information of the current source block.
  357. Returns a list
  358. (language body header-arguments-alist switches name function-args indent).
  359. Unless HEADER-VARS-ONLY is non-nil, any variable
  360. references provided in 'function call style' (i.e. in a
  361. parenthesised argument list following the src block name) are
  362. added to the header-arguments-alist."
  363. (let ((case-fold-search t) head info args indent)
  364. (if (setq head (org-babel-where-is-src-block-head))
  365. (save-excursion
  366. (goto-char head)
  367. (setq info (org-babel-parse-src-block-match))
  368. (setq indent (car (last info)))
  369. (setq info (butlast info))
  370. (forward-line -1)
  371. (if (looking-at
  372. (concat org-babel-source-name-regexp
  373. "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
  374. (progn
  375. (setq info (append info (list (org-babel-clean-text-properties
  376. (match-string 2)))))
  377. ;; Note that e.g. "name()" and "name( )" result in
  378. ;; ((:var . "")). We maintain that behaviour, and the
  379. ;; resulting non-nil sixth element is relied upon in
  380. ;; org-babel-exp-code to detect a functional-style
  381. ;; block in those cases. However, "name" without any
  382. ;; parentheses would result in the same thing, so we
  383. ;; explicitly avoid that.
  384. (if (setq args (match-string 4))
  385. (setq info
  386. (append info (list
  387. (mapcar
  388. (lambda (ref) (cons :var ref))
  389. (org-babel-ref-split-args args))))))
  390. (unless header-vars-only
  391. (setf (third info)
  392. (org-babel-merge-params (sixth info) (third info)))))
  393. (setq info (append info (list nil nil))))
  394. (append info (list indent)))
  395. (if (save-excursion ;; inline source block
  396. (re-search-backward "[ \f\t\n\r\v]" nil t)
  397. (looking-at org-babel-inline-src-block-regexp))
  398. (org-babel-parse-inline-src-block-match)
  399. nil)))) ;; indicate that no source block was found
  400. (defun org-babel-sha1-hash (&optional info)
  401. (interactive)
  402. (let* ((info (or info (org-babel-get-src-block-info)))
  403. (hash (sha1 (format "%s-%s" (mapconcat (lambda (arg) (format "%S" arg))
  404. (third info) ":")
  405. (second info)))))
  406. (when (interactive-p) (message hash))
  407. hash))
  408. (defun org-babel-result-hash (&optional info)
  409. (org-babel-where-is-src-block-result nil info)
  410. (org-babel-clean-text-properties (match-string 3)))
  411. (defun org-babel-hide-hash ()
  412. "Hide the hash in the current results line. Only the initial
  413. `org-babel-hash-show' characters of the hash will remain
  414. visible."
  415. (add-to-invisibility-spec '(org-babel-hide-hash . t))
  416. (save-excursion
  417. (when (and (re-search-forward org-babel-result-regexp nil t)
  418. (match-string 3))
  419. (let* ((start (match-beginning 3))
  420. (hide-start (+ org-babel-hash-show start))
  421. (end (match-end 3))
  422. (hash (match-string 3))
  423. ov1 ov2)
  424. (setq ov1 (make-overlay start hide-start))
  425. (setq ov2 (make-overlay hide-start end))
  426. (overlay-put ov2 'invisible 'org-babel-hide-hash)
  427. (overlay-put ov1 'babel-hash hash)))))
  428. (defun org-babel-hide-all-hashes ()
  429. "Hide the hash in the current buffer. Only the initial
  430. `org-babel-hash-show' characters of each hash will remain
  431. visible. This function should be called as part of the
  432. `org-mode-hook'."
  433. (save-excursion
  434. (while (re-search-forward org-babel-result-regexp nil t)
  435. (goto-char (match-beginning 0))
  436. (org-babel-hide-hash)
  437. (goto-char (match-end 0)))))
  438. (add-hook 'org-mode-hook 'org-babel-hide-all-hashes)
  439. (defun org-babel-hash-at-point (&optional point)
  440. "Return the value of the hash at `point'. The hash is also
  441. added as the last element of the kill ring. This can be called
  442. with C-c C-c."
  443. (interactive)
  444. (let ((hash (car (delq nil (mapcar
  445. (lambda (ol) (overlay-get ol 'babel-hash))
  446. (overlays-at (or point (point))))))))
  447. (when hash (kill-new hash) (message hash))))
  448. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
  449. (defun org-babel-result-hide-spec ()
  450. (add-to-invisibility-spec '(org-babel-hide-result . t)))
  451. (add-hook 'org-mode-hook 'org-babel-result-hide-spec)
  452. (defvar org-babel-hide-result-overlays nil
  453. "Overlays hiding results.")
  454. (defun org-babel-result-hide-all ()
  455. "Fold all results in the current buffer."
  456. (interactive)
  457. (org-babel-show-result-all)
  458. (save-excursion
  459. (while (re-search-forward org-babel-result-regexp nil t)
  460. (save-excursion (goto-char (match-beginning 0))
  461. (org-babel-hide-result-toggle-maybe)))))
  462. (defun org-babel-show-result-all ()
  463. "Unfold all results in the current buffer."
  464. (mapc 'delete-overlay org-babel-hide-result-overlays)
  465. (setq org-babel-hide-result-overlays nil))
  466. (defun org-babel-hide-result-toggle-maybe ()
  467. "Toggle visibility of result at point."
  468. (interactive)
  469. (let ((case-fold-search t))
  470. (if (save-excursion
  471. (beginning-of-line 1)
  472. (looking-at org-babel-result-regexp))
  473. (progn (org-babel-hide-result-toggle)
  474. t) ;; to signal that we took action
  475. nil))) ;; to signal that we did not
  476. (defun org-babel-hide-result-toggle (&optional force)
  477. "Toggle the visibility of the current result."
  478. (interactive)
  479. (save-excursion
  480. (beginning-of-line)
  481. (if (re-search-forward org-babel-result-regexp nil t)
  482. (let ((start (progn (beginning-of-line 2) (- (point) 1)))
  483. (end (progn (goto-char (- (org-babel-result-end) 1)) (point)))
  484. ov)
  485. (if (memq t (mapcar (lambda (overlay)
  486. (eq (overlay-get overlay 'invisible)
  487. 'org-babel-hide-result))
  488. (overlays-at start)))
  489. (if (or (not force) (eq force 'off))
  490. (mapc (lambda (ov)
  491. (when (member ov org-babel-hide-result-overlays)
  492. (setq org-babel-hide-result-overlays
  493. (delq ov org-babel-hide-result-overlays)))
  494. (when (eq (overlay-get ov 'invisible)
  495. 'org-babel-hide-result)
  496. (delete-overlay ov)))
  497. (overlays-at start)))
  498. (setq ov (make-overlay start end))
  499. (overlay-put ov 'invisible 'org-babel-hide-result)
  500. ;; make the block accessible to isearch
  501. (overlay-put
  502. ov 'isearch-open-invisible
  503. (lambda (ov)
  504. (when (member ov org-babel-hide-result-overlays)
  505. (setq org-babel-hide-result-overlays
  506. (delq ov org-babel-hide-result-overlays)))
  507. (when (eq (overlay-get ov 'invisible)
  508. 'org-babel-hide-result)
  509. (delete-overlay ov))))
  510. (push ov org-babel-hide-result-overlays)))
  511. (error "Not looking at a result line"))))
  512. ;; org-tab-after-check-for-cycling-hook
  513. (add-hook 'org-tab-first-hook 'org-babel-hide-result-toggle-maybe)
  514. ;; Remove overlays when changing major mode
  515. (add-hook 'org-mode-hook
  516. (lambda () (org-add-hook 'change-major-mode-hook
  517. 'org-babel-show-result-all 'append 'local)))
  518. (defmacro org-babel-map-source-blocks (file &rest body)
  519. "Evaluate BODY forms on each source-block in FILE."
  520. (declare (indent 1))
  521. `(let ((visited-p (get-file-buffer (expand-file-name ,file)))
  522. to-be-removed)
  523. (save-window-excursion
  524. (find-file ,file)
  525. (setq to-be-removed (current-buffer))
  526. (goto-char (point-min))
  527. (while (re-search-forward org-babel-src-block-regexp nil t)
  528. (goto-char (match-beginning 0))
  529. (save-match-data ,@body)
  530. (goto-char (match-end 0))))
  531. (unless visited-p
  532. (kill-buffer to-be-removed))))
  533. (defun org-babel-params-from-properties (&optional lang)
  534. "Return an association list of any source block params which
  535. may be specified in the properties of the current outline entry."
  536. (save-match-data
  537. (let (val sym)
  538. (delq nil
  539. (mapcar
  540. (lambda (header-arg)
  541. (and (setq val
  542. (or (condition-case nil
  543. (org-entry-get (point) header-arg t)
  544. (error nil))
  545. (cdr (assoc header-arg org-file-properties))))
  546. (cons (intern (concat ":" header-arg)) val)))
  547. (mapcar
  548. 'symbol-name
  549. (append
  550. org-babel-header-arg-names
  551. (progn
  552. (setq sym (intern (concat "org-babel-header-arg-names:" lang)))
  553. (and (boundp sym) (eval sym))))))))))
  554. (defun org-babel-parse-src-block-match ()
  555. (let* ((block-indentation (length (match-string 1)))
  556. (lang (org-babel-clean-text-properties (match-string 2)))
  557. (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
  558. (switches (match-string 3))
  559. (body (org-babel-clean-text-properties (match-string 5)))
  560. (preserve-indentation (or org-src-preserve-indentation
  561. (string-match "-i\\>" switches))))
  562. (list lang
  563. ;; get block body less properties, protective commas, and indentation
  564. (with-temp-buffer
  565. (save-match-data
  566. (insert (org-babel-strip-protective-commas body))
  567. (unless preserve-indentation (org-do-remove-indentation))
  568. (buffer-string)))
  569. (org-babel-merge-params
  570. org-babel-default-header-args
  571. (org-babel-params-from-properties lang)
  572. (if (boundp lang-headers) (eval lang-headers) nil)
  573. (org-babel-parse-header-arguments
  574. (org-babel-clean-text-properties (or (match-string 4) ""))))
  575. switches
  576. block-indentation)))
  577. (defun org-babel-parse-inline-src-block-match ()
  578. (let* ((lang (org-babel-clean-text-properties (match-string 2)))
  579. (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
  580. (list lang
  581. (org-babel-strip-protective-commas
  582. (org-babel-clean-text-properties (match-string 5)))
  583. (org-babel-merge-params
  584. org-babel-default-inline-header-args
  585. (org-babel-params-from-properties lang)
  586. (if (boundp lang-headers) (eval lang-headers) nil)
  587. (org-babel-parse-header-arguments
  588. (org-babel-clean-text-properties (or (match-string 4) "")))))))
  589. (defun org-babel-parse-header-arguments (arg-string)
  590. "Parse a string of header arguments returning an alist."
  591. (if (> (length arg-string) 0)
  592. (delq nil
  593. (mapcar
  594. (lambda (arg)
  595. (if (string-match
  596. "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)"
  597. arg)
  598. (cons (intern (concat ":" (match-string 1 arg)))
  599. (let ((raw (org-babel-chomp (match-string 2 arg))))
  600. (if (org-babel-number-p raw)
  601. raw (org-babel-read raw))))
  602. (cons (intern (concat ":" arg)) nil)))
  603. (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t)))))
  604. (defun org-babel-process-params (params)
  605. "Parse params and resolve references.
  606. Return a list (session vars result-params result-type colnames rownames)."
  607. (let* ((session (cdr (assoc :session params)))
  608. (vars-and-names (org-babel-disassemble-tables
  609. (org-babel-ref-variables params)
  610. (cdr (assoc :hlines params))
  611. (cdr (assoc :colnames params))
  612. (cdr (assoc :rownames params))))
  613. (vars (car vars-and-names))
  614. (colnames (cadr vars-and-names))
  615. (rownames (caddr vars-and-names))
  616. (result-params (split-string (or (cdr (assoc :results params)) "")))
  617. (result-type (cond ((member "output" result-params) 'output)
  618. ((member "value" result-params) 'value)
  619. (t 'value))))
  620. (list session vars result-params result-type colnames rownames)))
  621. ;; row and column names
  622. (defun org-babel-del-hlines (table)
  623. "Remove all 'hlines from TABLE."
  624. (remove 'hline table))
  625. (defun org-babel-get-colnames (table)
  626. "Return a cons cell, the `car' of which contains the TABLE
  627. less colnames, and the `cdr' of which contains a list of the
  628. column names"
  629. (if (equal 'hline (second table))
  630. (cons (cddr table) (car table))
  631. (cons (cdr table) (car table))))
  632. (defun org-babel-get-rownames (table)
  633. "Return a cons cell, the `car' of which contains the TABLE less
  634. colnames, and the `cdr' of which contains a list of the column
  635. names. Note: this function removes any hlines in TABLE"
  636. (flet ((trans (table) (apply #'mapcar* #'list table)))
  637. (let* ((width (apply 'max (mapcar (lambda (el) (if (listp el) (length el) 0)) table)))
  638. (table (trans (mapcar (lambda (row)
  639. (if (not (equal row 'hline))
  640. row
  641. (setq row '())
  642. (dotimes (n width) (setq row (cons 'hline row)))
  643. row))
  644. table))))
  645. (cons (mapcar (lambda (row) (if (equal (car row) 'hline) 'hline row))
  646. (trans (cdr table)))
  647. (remove 'hline (car table))))))
  648. (defun org-babel-put-colnames (table colnames)
  649. "Add COLNAMES to TABLE if they exist."
  650. (if colnames (apply 'list colnames 'hline table) table))
  651. (defun org-babel-put-rownames (table rownames)
  652. "Add ROWNAMES to TABLE if they exist."
  653. (if rownames
  654. (mapcar (lambda (row)
  655. (if (listp row)
  656. (cons (or (pop rownames) "") row)
  657. row)) table)
  658. table))
  659. (defun org-babel-pick-name (names selector)
  660. "Select one out of an alist of row or column names."
  661. (when names
  662. (if (and selector (symbolp selector) (not (equal t selector)))
  663. (cdr (assoc selector names))
  664. (if (integerp selector)
  665. (nth (- selector 1) names)
  666. (cdr (car (last names)))))))
  667. (defun org-babel-disassemble-tables (vars hlines colnames rownames)
  668. "Process the variables in VARS according to the HLINES,
  669. ROWNAMES and COLNAMES header arguments. Return a list consisting
  670. of the vars, cnames and rnames."
  671. (let (cnames rnames)
  672. (list
  673. (mapcar
  674. (lambda (var)
  675. (when (listp (cdr var))
  676. (when (and (not (equal colnames "no"))
  677. (or colnames (and (equal (second (cdr var)) 'hline)
  678. (not (member 'hline (cddr (cdr var)))))))
  679. (let ((both (org-babel-get-colnames (cdr var))))
  680. (setq cnames (cons (cons (car var) (cdr both))
  681. cnames))
  682. (setq var (cons (car var) (car both)))))
  683. (when (and rownames (not (equal rownames "no")))
  684. (let ((both (org-babel-get-rownames (cdr var))))
  685. (setq rnames (cons (cons (car var) (cdr both))
  686. rnames))
  687. (setq var (cons (car var) (car both)))))
  688. (when (and hlines (not (equal hlines "yes")))
  689. (setq var (cons (car var) (org-babel-del-hlines (cdr var))))))
  690. var)
  691. vars)
  692. cnames rnames)))
  693. (defun org-babel-reassemble-table (table colnames rownames)
  694. "Given a TABLE and set of COLNAMES and ROWNAMES add the names
  695. to the table for reinsertion to org-mode."
  696. (if (listp table)
  697. ((lambda (table)
  698. (if (and colnames (listp (car table)) (= (length (car table))
  699. (length colnames)))
  700. (org-babel-put-colnames table colnames) table))
  701. (if (and rownames (= (length table) (length rownames)))
  702. (org-babel-put-rownames table rownames) table))
  703. table))
  704. (defun org-babel-where-is-src-block-head ()
  705. "Return the point at the beginning of the current source
  706. block. Specifically at the beginning of the #+BEGIN_SRC line.
  707. If the point is not on a source block then return nil."
  708. (let ((initial (point)) top bottom)
  709. (or
  710. (save-excursion ;; on a source name line
  711. (beginning-of-line 1)
  712. (and (looking-at org-babel-source-name-regexp) (forward-line 1)
  713. (looking-at org-babel-src-block-regexp)
  714. (point)))
  715. (save-excursion ;; on a #+begin_src line
  716. (beginning-of-line 1)
  717. (and (looking-at org-babel-src-block-regexp)
  718. (point)))
  719. (save-excursion ;; inside a src block
  720. (and
  721. (re-search-backward "^[ \t]*#\\+begin_src" nil t) (setq top (point))
  722. (re-search-forward "^[ \t]*#\\+end_src" nil t) (setq bottom (point))
  723. (< top initial) (< initial bottom)
  724. (progn (goto-char top) (beginning-of-line 1)
  725. (looking-at org-babel-src-block-regexp))
  726. (point))))))
  727. (defun org-babel-goto-named-source-block (&optional name)
  728. "Go to a named source-code block."
  729. (interactive "ssource-block name: ")
  730. (let ((point (org-babel-find-named-block name)))
  731. (if point
  732. ;; taken from `org-open-at-point'
  733. (progn (goto-char point) (org-show-context))
  734. (message "source-code block '%s' not found in this buffer" name))))
  735. (defun org-babel-find-named-block (name)
  736. "Find a named source-code block.
  737. Return the location of the source block identified by source
  738. NAME, or nil if no such block exists. Set match data according to
  739. org-babel-named-src-block-regexp."
  740. (save-excursion
  741. (let ((case-fold-search t)
  742. (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
  743. (goto-char (point-min))
  744. (when (or (re-search-forward regexp nil t)
  745. (re-search-backward regexp nil t))
  746. (match-beginning 0)))))
  747. (defun org-babel-find-named-result (name)
  748. "Return the location of the result named NAME in the current
  749. buffer or nil if no such result exists."
  750. (save-excursion
  751. (goto-char (point-min))
  752. (when (re-search-forward
  753. (concat org-babel-result-regexp
  754. "[ \t]" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
  755. (beginning-of-line 0) (point))))
  756. (defun org-babel-where-is-src-block-result (&optional insert info hash indent)
  757. "Return the point at the beginning of the result of the current
  758. source block. Specifically at the beginning of the results line.
  759. If no result exists for this block then create a results line
  760. following the source block."
  761. (save-excursion
  762. (let* ((on-lob-line (progn (beginning-of-line 1)
  763. (looking-at org-babel-lob-one-liner-regexp)))
  764. (name (if on-lob-line
  765. (first (org-babel-lob-get-info))
  766. (fifth (or info (org-babel-get-src-block-info)))))
  767. (head (unless on-lob-line (org-babel-where-is-src-block-head))) end)
  768. (when head (goto-char head))
  769. (or (and name (org-babel-find-named-result name))
  770. (and (or on-lob-line (re-search-forward "^[ \t]*#\\+end_src" nil t))
  771. (progn (end-of-line 1)
  772. (if (eobp) (insert "\n") (forward-char 1))
  773. (setq end (point))
  774. (or (and (not name)
  775. (progn ;; unnamed results line already exists
  776. (re-search-forward "[^ \f\t\n\r\v]" nil t)
  777. (beginning-of-line 1)
  778. (looking-at
  779. (concat org-babel-result-regexp "\n"))))
  780. ;; or (with optional insert) back up and
  781. ;; make one ourselves
  782. (when insert
  783. (goto-char end)
  784. (if (looking-at "[\n\r]")
  785. (forward-char 1) (insert "\n"))
  786. (insert (concat
  787. (if indent
  788. (mapconcat
  789. (lambda (el) " ")
  790. (number-sequence 1 indent) "")
  791. "")
  792. "#+results"
  793. (when hash (concat "["hash"]"))
  794. ":"
  795. (when name (concat " " name)) "\n"))
  796. (beginning-of-line 0)
  797. (if hash (org-babel-hide-hash)) t)))
  798. (point))))))
  799. (defun org-babel-read-result ()
  800. "Read the result at `point' into emacs-lisp."
  801. (let ((case-fold-search t) result-string)
  802. (cond
  803. ((org-at-table-p) (org-babel-read-table))
  804. ((looking-at org-bracket-link-regexp) (org-babel-read-link))
  805. ((looking-at org-block-regexp) (org-babel-trim (match-string 4)))
  806. ((looking-at "^[ \t]*: ")
  807. (setq result-string
  808. (org-babel-trim
  809. (mapconcat (lambda (line)
  810. (if (and (> (length line) 1)
  811. (string-match "^[ \t]*: \\(.+\\)" line))
  812. (match-string 1 line)
  813. line))
  814. (split-string
  815. (buffer-substring
  816. (point) (org-babel-result-end)) "[\r\n]+")
  817. "\n")))
  818. (or (org-babel-number-p result-string) result-string))
  819. ((looking-at org-babel-result-regexp)
  820. (save-excursion (forward-line 1) (org-babel-read-result))))))
  821. (defun org-babel-read-table ()
  822. "Read the table at `point' into emacs-lisp."
  823. (mapcar (lambda (row)
  824. (if (and (symbolp row) (equal row 'hline)) row
  825. (mapcar #'org-babel-read row)))
  826. (org-table-to-lisp)))
  827. (defun org-babel-read-link ()
  828. "Read the link at `point' into emacs-lisp. If the path of the
  829. link is a file path it is expanded using `expand-file-name'."
  830. (let* ((case-fold-search t)
  831. (raw (and (looking-at org-bracket-link-regexp)
  832. (org-babel-clean-text-properties (match-string 1))))
  833. (type (and (string-match org-link-types-re raw)
  834. (match-string 1 raw))))
  835. (cond
  836. ((not type) (expand-file-name raw))
  837. ((string= type "file")
  838. (and (string-match "file\\(.*\\):\\(.+\\)" raw)
  839. (expand-file-name (match-string 2 raw))))
  840. (t raw))))
  841. (defun org-babel-insert-result (result &optional result-params info hash indent)
  842. "Insert RESULT into the current buffer after the end of the
  843. current source block. With optional argument RESULT-PARAMS
  844. controls insertion of results in the org-mode file.
  845. RESULT-PARAMS can take the following values...
  846. replace - (default option) insert results after the source block
  847. replacing any previously inserted results
  848. silent -- no results are inserted
  849. file ---- the results are interpreted as a file path, and are
  850. inserted into the buffer using the Org-mode file syntax
  851. raw ----- results are added directly to the org-mode file. This
  852. is a good option if you code block will output org-mode
  853. formatted text.
  854. org ----- this is the same as the 'raw' option
  855. html ---- results are added inside of a #+BEGIN_HTML block. This
  856. is a good option if you code block will output html
  857. formatted text.
  858. latex --- results are added inside of a #+BEGIN_LATEX block.
  859. This is a good option if you code block will output
  860. latex formatted text.
  861. code ---- the results are extracted in the syntax of the source
  862. code of the language being evaluated and are added
  863. inside of a #+BEGIN_SRC block with the source-code
  864. language set appropriately."
  865. (if (stringp result)
  866. (progn
  867. (setq result (org-babel-clean-text-properties result))
  868. (when (member "file" result-params)
  869. (setq result (org-babel-result-to-file result))))
  870. (unless (listp result) (setq result (format "%S" result))))
  871. (if (= (length result) 0)
  872. (if (member "value" result-params)
  873. (message "No result returned by source block")
  874. (message "Source block produced no output"))
  875. (if (and result-params (member "silent" result-params))
  876. (progn
  877. (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
  878. result)
  879. (when (and (stringp result) ;; ensure results end in a newline
  880. (not (or (string-equal (substring result -1) "\n")
  881. (string-equal (substring result -1) "\r"))))
  882. (setq result (concat result "\n")))
  883. (save-excursion
  884. (let ((existing-result (org-babel-where-is-src-block-result
  885. t info hash indent))
  886. (results-switches
  887. (cdr (assoc :results_switches (third info))))
  888. beg end)
  889. (when existing-result
  890. (goto-char existing-result)
  891. (save-excursion
  892. (re-search-forward "#" nil t)
  893. (setq indent (- (current-column) 1)))
  894. (forward-line 1)
  895. (setq beg (point))
  896. (cond
  897. ((member "replace" result-params)
  898. (delete-region (point) (org-babel-result-end)))
  899. ((member "append" result-params)
  900. (goto-char (org-babel-result-end)) (setq beg (point)))
  901. ((member "prepend" result-params) ;; already there
  902. )))
  903. (setq results-switches
  904. (if results-switches (concat " " results-switches) ""))
  905. (cond
  906. ;; assume the result is a table if it's not a string
  907. ((not (stringp result))
  908. (insert (concat (orgtbl-to-orgtbl
  909. (if (or (eq 'hline (car result))
  910. (and (listp (car result))
  911. (listp (cdr (car result)))))
  912. result (list result))
  913. '(:fmt (lambda (cell) (format "%s" cell)))) "\n"))
  914. (goto-char beg) (org-table-align))
  915. ((member "file" result-params)
  916. (insert result))
  917. ((member "html" result-params)
  918. (insert (format "#+BEGIN_HTML%s\n%s#+END_HTML\n"
  919. results-switches result)))
  920. ((member "latex" result-params)
  921. (insert (format "#+BEGIN_LaTeX%s\n%s#+END_LaTeX\n"
  922. results-switches result)))
  923. ((member "code" result-params)
  924. (insert (format "#+BEGIN_SRC %s%s\n%s#+END_SRC\n"
  925. lang results-switches result)))
  926. ((or (member "raw" result-params) (member "org" result-params))
  927. (save-excursion (insert result)) (if (org-at-table-p) (org-cycle)))
  928. (t
  929. (org-babel-examplize-region
  930. (point) (progn (insert result) (point)) results-switches)))
  931. ;; possibly indent the results to match the #+results line
  932. (setq end (if (listp result) (org-table-end) (point)))
  933. (when (and indent (> indent 0)
  934. ;; in this case `table-align' does the work for us
  935. (not (and (listp result)
  936. (member "append" result-params))))
  937. (indent-rigidly beg end indent))))
  938. (message "finished"))))
  939. (defun org-babel-result-to-org-string (result)
  940. "Return RESULT as a string in org-mode format. This function
  941. relies on `org-babel-insert-result'."
  942. (with-temp-buffer (org-babel-insert-result result) (buffer-string)))
  943. (defun org-babel-remove-result (&optional info)
  944. "Remove the result of the current source block."
  945. (interactive)
  946. (let ((location (org-babel-where-is-src-block-result nil info)) start)
  947. (when location
  948. (save-excursion
  949. (goto-char location) (setq start (point)) (forward-line 1)
  950. (delete-region start (org-babel-result-end))))))
  951. (defun org-babel-result-end ()
  952. "Return the point at the end of the current set of results"
  953. (save-excursion
  954. (if (org-at-table-p)
  955. (progn (goto-char (org-table-end)) (point))
  956. (let ((case-fold-search t))
  957. (cond
  958. ((looking-at "[ \t]*#\\+begin_latex")
  959. (search-forward "[ \t]*#\\+end_latex" nil t)
  960. (forward-line 1))
  961. ((looking-at "[ \t]*#\\+begin_html")
  962. (search-forward "[ \t]*#\\+end_html" nil t)
  963. (forward-line 1))
  964. ((looking-at "[ \t]*#\\+begin_example")
  965. (search-forward "[ \t]*#\\+end_example" nil t)
  966. (forward-line 1))
  967. ((looking-at "[ \t]*#\\+begin_src")
  968. (search-forward "[ \t]*#\\+end_src" nil t)
  969. (forward-line 1))
  970. (t (progn (while (looking-at "[ \t]*\\(: \\|\\[\\[\\)")
  971. (forward-line 1))))))
  972. (point))))
  973. (defun org-babel-result-to-file (result)
  974. "Convert RESULT into an `org-mode' link. If the
  975. `default-directory' is different from the containing file's
  976. directory then expand relative links."
  977. (format
  978. "[[file:%s]]"
  979. (if (and default-directory
  980. buffer-file-name
  981. (not (string= (expand-file-name default-directory)
  982. (expand-file-name
  983. (file-name-directory buffer-file-name)))))
  984. (expand-file-name result default-directory)
  985. result)))
  986. (defun org-babel-examplize-region (beg end &optional results-switches)
  987. "Comment out region using the ': ' org example quote."
  988. (interactive "*r")
  989. (let ((size (count-lines beg end)))
  990. (save-excursion
  991. (cond ((= size 0)
  992. (error (concat "This should be impossible:"
  993. "a newline was appended to result if missing")))
  994. ((< size org-babel-min-lines-for-block-output)
  995. (goto-char beg)
  996. (dotimes (n size)
  997. (beginning-of-line 1) (insert ": ") (forward-line 1)))
  998. (t
  999. (goto-char beg)
  1000. (insert (if results-switches
  1001. (format "#+begin_example%s\n" results-switches)
  1002. "#+begin_example\n"))
  1003. (forward-char (- end beg))
  1004. (insert "#+end_example\n"))))))
  1005. (defun org-babel-merge-params (&rest plists)
  1006. "Combine all parameter association lists in PLISTS. Later
  1007. elements of PLISTS override the values of previous element. This
  1008. takes into account some special considerations for certain
  1009. parameters when merging lists."
  1010. (let ((results-exclusive-groups
  1011. '(("file" "vector" "table" "scalar" "raw" "org"
  1012. "html" "latex" "code" "pp")
  1013. ("replace" "silent" "append" "prepend")
  1014. ("output" "value")))
  1015. (exports-exclusive-groups
  1016. '(("code" "results" "both" "none")))
  1017. params results exports tangle noweb cache vars var ref shebang comments)
  1018. (flet ((e-merge (exclusive-groups &rest result-params)
  1019. ;; maintain exclusivity of mutually exclusive parameters
  1020. (let (output)
  1021. (mapc (lambda (new-params)
  1022. (mapc (lambda (new-param)
  1023. (mapc (lambda (exclusive-group)
  1024. (when (member new-param exclusive-group)
  1025. (mapcar (lambda (excluded-param)
  1026. (setq output
  1027. (delete
  1028. excluded-param
  1029. output)))
  1030. exclusive-group)))
  1031. exclusive-groups)
  1032. (setq output (org-uniquify
  1033. (cons new-param output))))
  1034. new-params))
  1035. result-params)
  1036. output)))
  1037. (mapc (lambda (plist)
  1038. (mapc (lambda (pair)
  1039. (case (car pair)
  1040. (:var
  1041. ;; we want only one specification per variable
  1042. (when (string-match
  1043. (concat "^\\([^= \f\t\n\r\v]+\\)[ \t]*="
  1044. "[ \t]*\\([^\f\n\r\v]+\\)$") (cdr pair))
  1045. ;; TODO: When is this not true?
  1046. (setq var (intern (match-string 1 (cdr pair)))
  1047. ref (match-string 2 (cdr pair))
  1048. vars (cons (cons var ref)
  1049. (assq-delete-all var vars)))))
  1050. (:results
  1051. (setq results
  1052. (e-merge results-exclusive-groups
  1053. results (split-string (cdr pair)))))
  1054. (:file
  1055. (when (cdr pair)
  1056. (setq results (e-merge results-exclusive-groups
  1057. results '("file")))
  1058. (unless (or (member "both" exports)
  1059. (member "none" exports)
  1060. (member "code" exports))
  1061. (setq exports (e-merge exports-exclusive-groups
  1062. exports '("results"))))
  1063. (setq params
  1064. (cons pair
  1065. (assq-delete-all (car pair) params)))))
  1066. (:exports
  1067. (setq exports
  1068. (e-merge exports-exclusive-groups
  1069. exports (split-string (cdr pair)))))
  1070. (:tangle ;; take the latest -- always overwrite
  1071. (setq tangle (or (list (cdr pair)) tangle)))
  1072. (:noweb
  1073. (setq noweb
  1074. (e-merge '(("yes" "no")) noweb
  1075. (split-string (or (cdr pair) "")))))
  1076. (:cache
  1077. (setq cache
  1078. (e-merge '(("yes" "no")) cache
  1079. (split-string (or (cdr pair) "")))))
  1080. (:shebang ;; take the latest -- always overwrite
  1081. (setq shebang (or (list (cdr pair)) shebang)))
  1082. (:comments
  1083. (setq comments
  1084. (e-merge '(("yes" "no")) comments
  1085. (split-string (or (cdr pair) "")))))
  1086. (t ;; replace: this covers e.g. :session
  1087. (setq params
  1088. (cons pair
  1089. (assq-delete-all (car pair) params))))))
  1090. plist))
  1091. plists))
  1092. (setq vars (mapcar (lambda (pair) (format "%s=%s" (car pair) (cdr pair))) vars))
  1093. (while vars (setq params (cons (cons :var (pop vars)) params)))
  1094. (cons (cons :comments (mapconcat 'identity comments " "))
  1095. (cons (cons :shebang (mapconcat 'identity shebang " "))
  1096. (cons (cons :cache (mapconcat 'identity cache " "))
  1097. (cons (cons :noweb (mapconcat 'identity noweb " "))
  1098. (cons (cons :tangle (mapconcat 'identity tangle " "))
  1099. (cons (cons :exports
  1100. (mapconcat 'identity exports " "))
  1101. (cons
  1102. (cons :results
  1103. (mapconcat 'identity results " "))
  1104. params)))))))))
  1105. (defun org-babel-expand-noweb-references (&optional info parent-buffer)
  1106. "This function expands Noweb style references in the body of
  1107. the current source-code block. For example the following
  1108. reference would be replaced with the body of the source-code
  1109. block named 'example-block'.
  1110. <<example-block>>
  1111. Note that any text preceding the <<foo>> construct on a line will
  1112. be interposed between the lines of the replacement text. So for
  1113. example if <<foo>> is placed behind a comment, then the entire
  1114. replacement text will also be commented.
  1115. This function must be called from inside of the buffer containing
  1116. the source-code block which holds BODY.
  1117. In addition the following syntax can be used to insert the
  1118. results of evaluating the source-code block named 'example-block'.
  1119. <<example-block()>>
  1120. Any optional arguments can be passed to example-block by placing
  1121. the arguments inside the parenthesis following the convention
  1122. defined by `org-babel-lob'. For example
  1123. <<example-block(a=9)>>
  1124. would set the value of argument \"a\" equal to \"9\". Note that
  1125. these arguments are not evaluated in the current source-code
  1126. block but are passed literally to the \"example-block\"."
  1127. (let* ((parent-buffer (or parent-buffer (current-buffer)))
  1128. (info (or info (org-babel-get-src-block-info)))
  1129. (lang (first info))
  1130. (body (second info))
  1131. (new-body "") index source-name evaluate prefix)
  1132. (flet ((nb-add (text)
  1133. (setq new-body (concat new-body text))))
  1134. (with-temp-buffer
  1135. (insert body) (goto-char (point-min))
  1136. (setq index (point))
  1137. (while (and (re-search-forward "<<\\(.+?\\)>>" nil t))
  1138. (save-match-data (setf source-name (match-string 1)))
  1139. (save-match-data (setq evaluate (string-match "\(.*\)" source-name)))
  1140. (save-match-data
  1141. (setq prefix
  1142. (buffer-substring (match-beginning 0)
  1143. (save-excursion
  1144. (beginning-of-line 1) (point)))))
  1145. ;; add interval to new-body (removing noweb reference)
  1146. (goto-char (match-beginning 0))
  1147. (nb-add (buffer-substring index (point)))
  1148. (goto-char (match-end 0))
  1149. (setq index (point))
  1150. (nb-add (save-excursion
  1151. (set-buffer parent-buffer)
  1152. (mapconcat ;; interpose `prefix' between every line
  1153. #'identity
  1154. (split-string
  1155. (if evaluate
  1156. (let ((raw (org-babel-ref-resolve-reference
  1157. source-name nil)))
  1158. (if (stringp raw) raw (format "%S" raw)))
  1159. (save-restriction
  1160. (widen)
  1161. (let ((point (org-babel-find-named-block source-name)))
  1162. (if point
  1163. (save-excursion
  1164. (goto-char point)
  1165. (org-babel-trim
  1166. (org-babel-expand-noweb-references
  1167. (org-babel-get-src-block-info))))
  1168. ;; optionally raise an error if named
  1169. ;; source-block doesn't exist
  1170. (if (member lang org-babel-noweb-error-langs)
  1171. (error
  1172. (concat "<<%s>> could not be resolved "
  1173. "(see `org-babel-noweb-error-langs')")
  1174. source-name)
  1175. "")))))
  1176. "[\n\r]") (concat "\n" prefix)))))
  1177. (nb-add (buffer-substring index (point-max)))))
  1178. new-body))
  1179. (defun org-babel-error-notify (exit-code stderr)
  1180. (message (format "Shell command exited with code %d" exit-code))
  1181. (let ((buf (get-buffer-create "*Org-Babel Error Output*")))
  1182. (with-current-buffer buf
  1183. (goto-char (point-max))
  1184. (save-excursion (insert stderr)))
  1185. (display-buffer buf)))
  1186. (defun org-babel-clean-text-properties (text)
  1187. "Strip all properties from text return."
  1188. (when text
  1189. (set-text-properties 0 (length text) nil text) text))
  1190. (defun org-babel-strip-protective-commas (body)
  1191. "Strip protective commas from bodies of source blocks."
  1192. (replace-regexp-in-string "^,#" "#" body))
  1193. (defun org-babel-read (cell)
  1194. "Convert the string value of CELL to a number if appropriate.
  1195. Otherwise if cell looks like lisp (meaning it starts with a
  1196. \"(\" or a \"'\") then read it as lisp, otherwise return it
  1197. unmodified as a string.
  1198. This is taken almost directly from `org-read-prop'."
  1199. (if (and (stringp cell) (not (equal cell "")))
  1200. (or (org-babel-number-p cell)
  1201. (if (or (equal "(" (substring cell 0 1))
  1202. (equal "'" (substring cell 0 1))
  1203. (equal "`" (substring cell 0 1)))
  1204. (eval (read cell))
  1205. (progn (set-text-properties 0 (length cell) nil cell) cell)))
  1206. cell))
  1207. (defun org-babel-number-p (string)
  1208. "Return t if STRING represents a number"
  1209. (if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string)
  1210. (= (length (substring string (match-beginning 0)
  1211. (match-end 0)))
  1212. (length string)))
  1213. (string-to-number string)))
  1214. (defun org-babel-import-elisp-from-file (file-name)
  1215. "Read the results located at FILE-NAME into an elisp table. If
  1216. the table is trivial, then return it as a scalar."
  1217. (let (result)
  1218. (save-window-excursion
  1219. (with-temp-buffer
  1220. (condition-case nil
  1221. (progn
  1222. (org-table-import file-name nil)
  1223. (delete-file file-name)
  1224. (setq result (mapcar (lambda (row)
  1225. (mapcar #'org-babel-string-read row))
  1226. (org-table-to-lisp))))
  1227. (error nil)))
  1228. (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
  1229. (if (consp (car result))
  1230. (if (null (cdr (car result)))
  1231. (caar result)
  1232. result)
  1233. (car result))
  1234. result))))
  1235. (defun org-babel-string-read (cell)
  1236. "Strip nested \"s from around strings."
  1237. (org-babel-read (or (and (stringp cell)
  1238. (string-match "\\\"\\(.+\\)\\\"" cell)
  1239. (match-string 1 cell))
  1240. cell)))
  1241. (defun org-babel-reverse-string (string)
  1242. (apply 'string (reverse (string-to-list string))))
  1243. (defun org-babel-chomp (string &optional regexp)
  1244. "Remove any trailing space or carriage returns characters from
  1245. STRING. Default regexp used is \"[ \f\t\n\r\v]\" but can be
  1246. overwritten by specifying a regexp as a second argument."
  1247. (let ((regexp (or regexp "[ \f\t\n\r\v]")))
  1248. (while (and (> (length string) 0)
  1249. (string-match regexp (substring string -1)))
  1250. (setq string (substring string 0 -1)))
  1251. string))
  1252. (defun org-babel-trim (string &optional regexp)
  1253. "Like `org-babel-chomp' only it runs on both the front and back of the string"
  1254. (org-babel-chomp (org-babel-reverse-string
  1255. (org-babel-chomp (org-babel-reverse-string string) regexp))
  1256. regexp))
  1257. (defun org-babel-tramp-handle-call-process-region
  1258. (start end program &optional delete buffer display &rest args)
  1259. "Use tramp to handle call-process-region.
  1260. Fixes a bug in `tramp-handle-call-process-region'."
  1261. (if (and (featurep 'tramp) (file-remote-p default-directory))
  1262. (let ((tmpfile (tramp-compat-make-temp-file "")))
  1263. (write-region start end tmpfile)
  1264. (when delete (delete-region start end))
  1265. (unwind-protect
  1266. ;; (apply 'call-process program tmpfile buffer display args)
  1267. ;; bug in tramp
  1268. (apply 'process-file program tmpfile buffer display args)
  1269. (delete-file tmpfile)))
  1270. ;; call-process-region-original is the original emacs definition. It
  1271. ;; is in scope from the let binding in org-babel-execute-src-block
  1272. (apply call-process-region-original
  1273. start end program delete buffer display args)))
  1274. (defun org-babel-maybe-remote-file (file)
  1275. (if (file-remote-p default-directory)
  1276. (let* ((vec (tramp-dissect-file-name default-directory))
  1277. (user (tramp-file-name-user vec))
  1278. (host (tramp-file-name-host vec)))
  1279. (concat "/" user (when user "@") host ":" file))
  1280. file))
  1281. (defun org-babel-shell-command-on-region (start end command
  1282. &optional output-buffer replace
  1283. error-buffer display-error-buffer)
  1284. "Execute string COMMAND in inferior shell with region as input.
  1285. Fixes bugs in the emacs 23.1.1 version of `shell-command-on-region'
  1286. Normally display output (if any) in temp buffer `*Shell Command Output*';
  1287. Prefix arg means replace the region with it. Return the exit code of
  1288. COMMAND.
  1289. To specify a coding system for converting non-ASCII characters in
  1290. the input and output to the shell command, use
  1291. \\[universal-coding-system-argument] before this command. By
  1292. default, the input (from the current buffer) is encoded in the
  1293. same coding system that will be used to save the file,
  1294. `buffer-file-coding-system'. If the output is going to replace
  1295. the region, then it is decoded from that same coding system.
  1296. The noninteractive arguments are START, END, COMMAND,
  1297. OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER.
  1298. Noninteractive callers can specify coding systems by binding
  1299. `coding-system-for-read' and `coding-system-for-write'.
  1300. If the command generates output, the output may be displayed
  1301. in the echo area or in a buffer.
  1302. If the output is short enough to display in the echo area
  1303. \(determined by the variable `max-mini-window-height' if
  1304. `resize-mini-windows' is non-nil), it is shown there. Otherwise
  1305. it is displayed in the buffer `*Shell Command Output*'. The output
  1306. is available in that buffer in both cases.
  1307. If there is output and an error, a message about the error
  1308. appears at the end of the output.
  1309. If there is no output, or if output is inserted in the current buffer,
  1310. then `*Shell Command Output*' is deleted.
  1311. If the optional fourth argument OUTPUT-BUFFER is non-nil,
  1312. that says to put the output in some other buffer.
  1313. If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
  1314. If OUTPUT-BUFFER is not a buffer and not nil,
  1315. insert output in the current buffer.
  1316. In either case, the output is inserted after point (leaving mark after it).
  1317. If REPLACE, the optional fifth argument, is non-nil, that means insert
  1318. the output in place of text from START to END, putting point and mark
  1319. around it.
  1320. If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
  1321. or buffer name to which to direct the command's standard error output.
  1322. If it is nil, error output is mingled with regular output.
  1323. If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
  1324. were any errors. (This is always t, interactively.)
  1325. In an interactive call, the variable `shell-command-default-error-buffer'
  1326. specifies the value of ERROR-BUFFER."
  1327. (interactive (let (string)
  1328. (unless (mark)
  1329. (error "The mark is not set now, so there is no region"))
  1330. ;; Do this before calling region-beginning
  1331. ;; and region-end, in case subprocess output
  1332. ;; relocates them while we are in the minibuffer.
  1333. (setq string (read-shell-command "Shell command on region: "))
  1334. ;; call-interactively recognizes region-beginning and
  1335. ;; region-end specially, leaving them in the history.
  1336. (list (region-beginning) (region-end)
  1337. string
  1338. current-prefix-arg
  1339. current-prefix-arg
  1340. shell-command-default-error-buffer
  1341. t)))
  1342. (let ((error-file
  1343. (if error-buffer
  1344. (make-temp-file
  1345. (expand-file-name "scor"
  1346. (or (unless (featurep 'xemacs)
  1347. small-temporary-file-directory)
  1348. temporary-file-directory)))
  1349. nil))
  1350. exit-status)
  1351. (if (or replace
  1352. (and output-buffer
  1353. (not (or (bufferp output-buffer) (stringp output-buffer)))))
  1354. ;; Replace specified region with output from command.
  1355. (let ((swap (and replace (< start end))))
  1356. ;; Don't muck with mark unless REPLACE says we should.
  1357. (goto-char start)
  1358. (and replace (push-mark (point) 'nomsg))
  1359. (setq exit-status
  1360. (call-process-region start end shell-file-name t
  1361. (if error-file
  1362. (list output-buffer error-file)
  1363. t)
  1364. nil shell-command-switch command))
  1365. ;; It is rude to delete a buffer which the command is not using.
  1366. ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
  1367. ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
  1368. ;; (kill-buffer shell-buffer)))
  1369. ;; Don't muck with mark unless REPLACE says we should.
  1370. (and replace swap (exchange-point-and-mark)))
  1371. ;; No prefix argument: put the output in a temp buffer,
  1372. ;; replacing its entire contents.
  1373. (let ((buffer (get-buffer-create
  1374. (or output-buffer "*Shell Command Output*"))))
  1375. (unwind-protect
  1376. (if (eq buffer (current-buffer))
  1377. ;; If the input is the same buffer as the output,
  1378. ;; delete everything but the specified region,
  1379. ;; then replace that region with the output.
  1380. (progn (setq buffer-read-only nil)
  1381. (delete-region (max start end) (point-max))
  1382. (delete-region (point-min) (min start end))
  1383. (setq exit-status
  1384. (call-process-region (point-min) (point-max)
  1385. shell-file-name t
  1386. (if error-file
  1387. (list t error-file)
  1388. t)
  1389. nil shell-command-switch
  1390. command)))
  1391. ;; Clear the output buffer, then run the command with
  1392. ;; output there.
  1393. (let ((directory default-directory))
  1394. (save-excursion
  1395. (set-buffer buffer)
  1396. (setq buffer-read-only nil)
  1397. (if (not output-buffer)
  1398. (setq default-directory directory))
  1399. (erase-buffer)))
  1400. (setq exit-status
  1401. (call-process-region start end shell-file-name nil
  1402. (if error-file
  1403. (list buffer error-file)
  1404. buffer)
  1405. nil shell-command-switch command)))
  1406. ;; Report the output.
  1407. (with-current-buffer buffer
  1408. (setq mode-line-process
  1409. (cond ((null exit-status)
  1410. " - Error")
  1411. ((stringp exit-status)
  1412. (format " - Signal [%s]" exit-status))
  1413. ((not (equal 0 exit-status))
  1414. (format " - Exit [%d]" exit-status)))))
  1415. (if (with-current-buffer buffer (> (point-max) (point-min)))
  1416. ;; There's some output, display it
  1417. (display-message-or-buffer buffer)
  1418. ;; No output; error?
  1419. (let ((output
  1420. (if (and error-file
  1421. (< 0 (nth 7 (file-attributes error-file))))
  1422. "some error output"
  1423. "no output")))
  1424. (cond ((null exit-status)
  1425. (message "(Shell command failed with error)"))
  1426. ((equal 0 exit-status)
  1427. (message "(Shell command succeeded with %s)"
  1428. output))
  1429. ((stringp exit-status)
  1430. (message "(Shell command killed by signal %s)"
  1431. exit-status))
  1432. (t
  1433. (message "(Shell command failed with code %d and %s)"
  1434. exit-status output))))
  1435. ;; Don't kill: there might be useful info in the undo-log.
  1436. ;; (kill-buffer buffer)
  1437. ))))
  1438. (when (and error-file (file-exists-p error-file))
  1439. (if (< 0 (nth 7 (file-attributes error-file)))
  1440. (with-current-buffer (get-buffer-create error-buffer)
  1441. (let ((pos-from-end (- (point-max) (point))))
  1442. (or (bobp)
  1443. (insert "\f\n"))
  1444. ;; Do no formatting while reading error file,
  1445. ;; because that can run a shell command, and we
  1446. ;; don't want that to cause an infinite recursion.
  1447. (format-insert-file error-file nil)
  1448. ;; Put point after the inserted errors.
  1449. (goto-char (- (point-max) pos-from-end)))
  1450. (and display-error-buffer
  1451. (display-buffer (current-buffer)))))
  1452. (delete-file error-file))
  1453. exit-status))
  1454. (provide 'org-babel)
  1455. ;;; org-babel.el ends here