ob.el 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  1. ;;; ob.el --- working with code blocks in org-mode
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte, Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.5
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; See the online documentation for more information
  20. ;;
  21. ;; http://orgmode.org/worg/org-contrib/babel/
  22. ;;; Code:
  23. (eval-when-compile
  24. (require 'cl))
  25. (require 'ob-eval)
  26. (require 'org-macs)
  27. (defvar org-babel-call-process-region-original)
  28. (defvar org-src-lang-modes)
  29. (defvar org-babel-library-of-babel)
  30. (declare-function show-all "outline" ())
  31. (declare-function tramp-compat-make-temp-file "tramp-compat"
  32. (filename &optional dir-flag))
  33. (declare-function tramp-dissect-file-name "tramp" (name &optional nodefault))
  34. (declare-function tramp-file-name-user "tramp" (vec))
  35. (declare-function tramp-file-name-host "tramp" (vec))
  36. (declare-function with-parsed-tramp-file-name "tramp" (filename var &rest body))
  37. (declare-function org-icompleting-read "org" (&rest args))
  38. (declare-function org-edit-src-code "org-src"
  39. (&optional context code edit-buffer-name quietp))
  40. (declare-function org-edit-src-exit "org-src" (&optional context))
  41. (declare-function org-open-at-point "org" (&optional in-emacs reference-buffer))
  42. (declare-function org-save-outline-visibility "org" (use-markers &rest body))
  43. (declare-function org-outline-overlay-data "org" (&optional use-markers))
  44. (declare-function org-set-outline-overlay-data "org" (data))
  45. (declare-function org-narrow-to-subtree "org" ())
  46. (declare-function org-entry-get "org"
  47. (pom property &optional inherit literal-nil))
  48. (declare-function org-make-options-regexp "org" (kwds &optional extra))
  49. (declare-function org-do-remove-indentation "org" (&optional n))
  50. (declare-function org-show-context "org" (&optional key))
  51. (declare-function org-at-table-p "org" (&optional table-type))
  52. (declare-function org-cycle "org" (&optional arg))
  53. (declare-function org-uniquify "org" (list))
  54. (declare-function org-current-level "org" ())
  55. (declare-function org-table-import "org-table" (file arg))
  56. (declare-function org-add-hook "org-compat"
  57. (hook function &optional append local))
  58. (declare-function org-table-align "org-table" ())
  59. (declare-function org-table-end "org-table" (&optional table-type))
  60. (declare-function orgtbl-to-generic "org-table" (table params))
  61. (declare-function orgtbl-to-orgtbl "org-table" (table params))
  62. (declare-function org-babel-tangle-comment-links "ob-tangle" (&optional info))
  63. (declare-function org-babel-lob-get-info "ob-lob" nil)
  64. (declare-function org-babel-ref-split-args "ob-ref" (arg-string))
  65. (declare-function org-babel-ref-parse "ob-ref" (assignment))
  66. (declare-function org-babel-ref-resolve "ob-ref" (ref))
  67. (declare-function org-babel-lob-execute-maybe "ob-lob" ())
  68. (declare-function org-number-sequence "org-compat" (from &optional to inc))
  69. (declare-function org-at-item-p "org-list" ())
  70. (declare-function org-list-parse-list "org-list" (&optional delete))
  71. (declare-function org-list-to-generic "org-list" (LIST PARAMS))
  72. (declare-function org-list-struct "org-list" ())
  73. (declare-function org-list-prevs-alist "org-list" (struct))
  74. (declare-function org-list-get-list-end "org-list" (item struct prevs))
  75. (defgroup org-babel nil
  76. "Code block evaluation and management in `org-mode' documents."
  77. :tag "Babel"
  78. :group 'org)
  79. (defcustom org-confirm-babel-evaluate t
  80. "Confirm before evaluation.
  81. Require confirmation before interactively evaluating code
  82. blocks in Org-mode buffers. The default value of this variable
  83. is t, meaning confirmation is required for any code block
  84. evaluation. This variable can be set to nil to inhibit any
  85. future confirmation requests. This variable can also be set to a
  86. function which takes two arguments the language of the code block
  87. and the body of the code block. Such a function should then
  88. return a non-nil value if the user should be prompted for
  89. execution or nil if no prompt is required.
  90. Warning: Disabling confirmation may result in accidental
  91. evaluation of potentially harmful code. It may be advisable
  92. remove code block execution from C-c C-c as further protection
  93. against accidental code block evaluation. The
  94. `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can be used to
  95. remove code block execution from the C-c C-c keybinding."
  96. :group 'org-babel
  97. :type '(choice boolean function))
  98. ;; don't allow this variable to be changed through file settings
  99. (put 'org-confirm-babel-evaluate 'safe-local-variable (lambda (x) (eq x t)))
  100. (defcustom org-babel-no-eval-on-ctrl-c-ctrl-c nil
  101. "Remove code block evaluation from the C-c C-c key binding."
  102. :group 'org-babel
  103. :type 'boolean)
  104. (defvar org-babel-src-name-regexp
  105. "^[ \t]*#\\+\\(srcname\\|source\\|function\\):[ \t]*"
  106. "Regular expression used to match a source name line.")
  107. (defvar org-babel-multi-line-header-regexp
  108. "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
  109. "Regular expression used to match multi-line header arguments.")
  110. (defvar org-babel-src-name-w-name-regexp
  111. (concat org-babel-src-name-regexp
  112. "\\("
  113. org-babel-multi-line-header-regexp
  114. "\\)*"
  115. "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)")
  116. "Regular expression matching source name lines with a name.")
  117. (defvar org-babel-src-block-regexp
  118. (concat
  119. ;; (1) indentation (2) lang
  120. "^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
  121. ;; (3) switches
  122. "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
  123. ;; (4) header arguments
  124. "\\([^\n]*\\)\n"
  125. ;; (5) body
  126. "\\([^\000]*?\\)[ \t]*#\\+end_src")
  127. "Regexp used to identify code blocks.")
  128. (defvar org-babel-inline-src-block-regexp
  129. (concat
  130. ;; (1) replacement target (2) lang
  131. "[^-[:alnum:]]\\(src_\\([^ \f\t\n\r\v]+\\)"
  132. ;; (3,4) (unused, headers)
  133. "\\(\\|\\[\\(.*?\\)\\]\\)"
  134. ;; (5) body
  135. "{\\([^\f\n\r\v]+?\\)}\\)")
  136. "Regexp used to identify inline src-blocks.")
  137. (defun org-babel-get-header (params key &optional others)
  138. "Select only header argument of type KEY from a list.
  139. Optional argument OTHERS indicates that only the header that do
  140. not match KEY should be returned."
  141. (delq nil
  142. (mapcar
  143. (lambda (p) (when (funcall (if others #'not #'identity) (eq (car p) key)) p))
  144. params)))
  145. (defun org-babel-get-src-block-info (&optional light)
  146. "Get information on the current source block.
  147. Optional argument LIGHT does not resolve remote variable
  148. references; a process which could likely result in the execution
  149. of other code blocks.
  150. Returns a list
  151. (language body header-arguments-alist switches name indent)."
  152. (let ((case-fold-search t) head info name indent)
  153. ;; full code block
  154. (if (setq head (org-babel-where-is-src-block-head))
  155. (save-excursion
  156. (goto-char head)
  157. (setq info (org-babel-parse-src-block-match))
  158. (setq indent (car (last info)))
  159. (setq info (butlast info))
  160. (while (and (forward-line -1)
  161. (looking-at org-babel-multi-line-header-regexp))
  162. (setf (nth 2 info)
  163. (org-babel-merge-params
  164. (nth 2 info)
  165. (org-babel-parse-header-arguments (match-string 1)))))
  166. (when (looking-at org-babel-src-name-w-name-regexp)
  167. (setq name (org-babel-clean-text-properties (match-string 4)))
  168. (when (match-string 6)
  169. (setf (nth 2 info) ;; merge functional-syntax vars and header-args
  170. (org-babel-merge-params
  171. (mapcar (lambda (ref) (cons :var ref))
  172. (org-babel-ref-split-args (match-string 6)))
  173. (nth 2 info))))))
  174. ;; inline source block
  175. (when (save-excursion (re-search-backward "[ \f\t\n\r\v]" nil t)
  176. (looking-at org-babel-inline-src-block-regexp))
  177. (setq info (org-babel-parse-inline-src-block-match))))
  178. ;; resolve variable references and add summary parameters
  179. (when (and info (not light))
  180. (setf (nth 2 info) (org-babel-process-params (nth 2 info))))
  181. (when info (append info (list name indent)))))
  182. (defun org-babel-confirm-evaluate (info)
  183. "Confirm evaluation of the code block INFO.
  184. This behavior can be suppressed by setting the value of
  185. `org-confirm-babel-evaluate' to nil, in which case all future
  186. interactive code block evaluations will proceed without any
  187. confirmation from the user.
  188. Note disabling confirmation may result in accidental evaluation
  189. of potentially harmful code."
  190. (let* ((eval (or (cdr (assoc :eval (nth 2 info)))
  191. (when (assoc :noeval (nth 2 info)) "no")))
  192. (query (cond ((equal eval "query") t)
  193. ((functionp org-confirm-babel-evaluate)
  194. (funcall org-confirm-babel-evaluate
  195. (nth 0 info) (nth 1 info)))
  196. (t org-confirm-babel-evaluate))))
  197. (if (or (equal eval "never") (equal eval "no")
  198. (and query
  199. (not (yes-or-no-p
  200. (format "Evaluate this%scode block%son your system? "
  201. (if info (format " %s " (nth 0 info)) " ")
  202. (if (nth 4 info)
  203. (format " (%s) " (nth 4 info)) " "))))))
  204. (prog1 nil (message "Evaluation %s"
  205. (if (or (equal eval "never") (equal eval "no"))
  206. "Disabled" "Aborted")))
  207. t)))
  208. ;;;###autoload
  209. (defun org-babel-execute-safely-maybe ()
  210. (unless org-babel-no-eval-on-ctrl-c-ctrl-c
  211. (org-babel-execute-maybe)))
  212. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-safely-maybe)
  213. ;;;###autoload
  214. (defun org-babel-execute-maybe ()
  215. (interactive)
  216. (or (org-babel-execute-src-block-maybe)
  217. (org-babel-lob-execute-maybe)))
  218. (defun org-babel-execute-src-block-maybe ()
  219. "Conditionally execute a source block.
  220. Detect if this is context for a Babel src-block and if so
  221. then run `org-babel-execute-src-block'."
  222. (interactive)
  223. (let ((info (org-babel-get-src-block-info)))
  224. (if info
  225. (progn (org-babel-eval-wipe-error-buffer)
  226. (org-babel-execute-src-block current-prefix-arg info) t) nil)))
  227. ;;;###autoload
  228. (defun org-babel-expand-src-block-maybe ()
  229. "Conditionally expand a source block.
  230. Detect if this is context for a org-babel src-block and if so
  231. then run `org-babel-expand-src-block'."
  232. (interactive)
  233. (let ((info (org-babel-get-src-block-info)))
  234. (if info
  235. (progn (org-babel-expand-src-block current-prefix-arg info) t)
  236. nil)))
  237. ;;;###autoload
  238. (defun org-babel-load-in-session-maybe ()
  239. "Conditionally load a source block in a session.
  240. Detect if this is context for a org-babel src-block and if so
  241. then run `org-babel-load-in-session'."
  242. (interactive)
  243. (let ((info (org-babel-get-src-block-info)))
  244. (if info
  245. (progn (org-babel-load-in-session current-prefix-arg info) t)
  246. nil)))
  247. (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
  248. ;;;###autoload
  249. (defun org-babel-pop-to-session-maybe ()
  250. "Conditionally pop to a session.
  251. Detect if this is context for a org-babel src-block and if so
  252. then run `org-babel-pop-to-session'."
  253. (interactive)
  254. (let ((info (org-babel-get-src-block-info)))
  255. (if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil)))
  256. (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
  257. (defconst org-babel-header-arg-names
  258. '(cache cmdline colnames dir exports file noweb results
  259. session tangle var eval noeval comments no-expand shebang
  260. padline noweb-ref)
  261. "Common header arguments used by org-babel.
  262. Note that individual languages may define their own language
  263. specific header arguments as well.")
  264. (defvar org-babel-default-header-args
  265. '((:session . "none") (:results . "replace") (:exports . "code")
  266. (:cache . "no") (:noweb . "no") (:hlines . "no") (:tangle . "no")
  267. (:padnewline . "yes"))
  268. "Default arguments to use when evaluating a source block.")
  269. (defvar org-babel-default-inline-header-args
  270. '((:session . "none") (:results . "replace") (:exports . "results"))
  271. "Default arguments to use when evaluating an inline source block.")
  272. (defvar org-babel-data-names '("TBLNAME" "RESNAME" "RESULTS" "DATA"))
  273. (defvar org-babel-result-regexp
  274. (concat "^[ \t]*#\\+"
  275. (regexp-opt org-babel-data-names t)
  276. "\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:[ \t]*")
  277. "Regular expression used to match result lines.
  278. If the results are associated with a hash key then the hash will
  279. be saved in the second match data.")
  280. (defvar org-babel-result-w-name-regexp
  281. (concat org-babel-result-regexp
  282. "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
  283. (defvar org-babel-min-lines-for-block-output 10
  284. "The minimum number of lines for block output.
  285. If number of lines of output is equal to or exceeds this
  286. value, the output is placed in a #+begin_example...#+end_example
  287. block. Otherwise the output is marked as literal by inserting
  288. colons at the starts of the lines. This variable only takes
  289. effect if the :results output option is in effect.")
  290. (defvar org-babel-noweb-error-langs nil
  291. "Languages for which Babel will raise literate programming errors.
  292. List of languages for which errors should be raised when the
  293. source code block satisfying a noweb reference in this language
  294. can not be resolved.")
  295. (defvar org-babel-hash-show 4
  296. "Number of initial characters to show of a hidden results hash.")
  297. (defvar org-babel-after-execute-hook nil
  298. "Hook for functions to be called after `org-babel-execute-src-block'")
  299. (defun org-babel-named-src-block-regexp-for-name (name)
  300. "This generates a regexp used to match a src block named NAME."
  301. (concat org-babel-src-name-regexp (regexp-quote name) "[ \t\n]*"
  302. (substring org-babel-src-block-regexp 1)))
  303. ;;; functions
  304. (defvar call-process-region)
  305. ;;;###autoload
  306. (defun org-babel-execute-src-block (&optional arg info params)
  307. "Execute the current source code block.
  308. Insert the results of execution into the buffer. Source code
  309. execution and the collection and formatting of results can be
  310. controlled through a variety of header arguments.
  311. With prefix argument ARG, force re-execution even if a an
  312. existing result cached in the buffer would otherwise have been
  313. returned.
  314. Optionally supply a value for INFO in the form returned by
  315. `org-babel-get-src-block-info'.
  316. Optionally supply a value for PARAMS which will be merged with
  317. the header arguments specified at the front of the source code
  318. block."
  319. (interactive)
  320. (let ((info (or info (org-babel-get-src-block-info))))
  321. (when (org-babel-confirm-evaluate info)
  322. (let* ((lang (nth 0 info))
  323. (params (if params
  324. (org-babel-process-params
  325. (org-babel-merge-params (nth 2 info) params))
  326. (nth 2 info)))
  327. (cache? (and (not arg) (cdr (assoc :cache params))
  328. (string= "yes" (cdr (assoc :cache params)))))
  329. (result-params (cdr (assoc :result-params params)))
  330. (new-hash (when cache? (org-babel-sha1-hash info)))
  331. (old-hash (when cache? (org-babel-result-hash info)))
  332. (body (setf (nth 1 info)
  333. (let ((noweb (cdr (assoc :noweb params))))
  334. (if (and noweb
  335. (or (string= "yes" noweb)
  336. (string= "tangle" noweb)))
  337. (org-babel-expand-noweb-references info)
  338. (nth 1 info)))))
  339. (dir (cdr (assoc :dir params)))
  340. (default-directory
  341. (or (and dir (file-name-as-directory dir)) default-directory))
  342. (org-babel-call-process-region-original
  343. (if (boundp 'org-babel-call-process-region-original)
  344. org-babel-call-process-region-original
  345. (symbol-function 'call-process-region)))
  346. (indent (car (last info)))
  347. result cmd)
  348. (unwind-protect
  349. (flet ((call-process-region (&rest args)
  350. (apply 'org-babel-tramp-handle-call-process-region args)))
  351. (flet ((lang-check (f)
  352. (let ((f (intern (concat "org-babel-execute:" f))))
  353. (when (fboundp f) f))))
  354. (setq cmd
  355. (or (lang-check lang)
  356. (lang-check (symbol-name
  357. (cdr (assoc lang org-src-lang-modes))))
  358. (error "No org-babel-execute function for %s!" lang))))
  359. (if (and (not arg) new-hash (equal new-hash old-hash))
  360. (save-excursion ;; return cached result
  361. (goto-char (org-babel-where-is-src-block-result nil info))
  362. (end-of-line 1) (forward-char 1)
  363. (setq result (org-babel-read-result))
  364. (message (replace-regexp-in-string
  365. "%" "%%" (format "%S" result))) result)
  366. (message "executing %s code block%s..."
  367. (capitalize lang)
  368. (if (nth 4 info) (format " (%s)" (nth 4 info)) ""))
  369. (setq result
  370. ((lambda (result)
  371. (if (and (eq (cdr (assoc :result-type params)) 'value)
  372. (or (member "vector" result-params)
  373. (member "table" result-params))
  374. (not (listp result)))
  375. (list (list result)) result))
  376. (funcall cmd body params)))
  377. ;; if non-empty result and :file then write to :file
  378. (when (cdr (assoc :file params))
  379. (when result
  380. (with-temp-file (cdr (assoc :file params))
  381. (insert
  382. (org-babel-format-result
  383. result (cdr (assoc :sep (nth 2 info)))))))
  384. (setq result (cdr (assoc :file params))))
  385. (org-babel-insert-result
  386. result result-params info new-hash indent lang)
  387. (run-hooks 'org-babel-after-execute-hook)
  388. result))
  389. (setq call-process-region 'org-babel-call-process-region-original))))))
  390. (defun org-babel-expand-body:generic (body params &optional var-lines)
  391. "Expand BODY with PARAMS.
  392. Expand a block of code with org-babel according to it's header
  393. arguments. This generic implementation of body expansion is
  394. called for languages which have not defined their own specific
  395. org-babel-expand-body:lang function."
  396. (mapconcat #'identity (append var-lines (list body)) "\n"))
  397. ;;;###autoload
  398. (defun org-babel-expand-src-block (&optional arg info params)
  399. "Expand the current source code block.
  400. Expand according to the source code block's header
  401. arguments and pop open the results in a preview buffer."
  402. (interactive)
  403. (let* ((info (or info (org-babel-get-src-block-info)))
  404. (lang (nth 0 info))
  405. (params (setf (nth 2 info)
  406. (sort (org-babel-merge-params (nth 2 info) params)
  407. (lambda (el1 el2) (string< (symbol-name (car el1))
  408. (symbol-name (car el2)))))))
  409. (body (setf (nth 1 info)
  410. (if (and (cdr (assoc :noweb params))
  411. (string= "yes" (cdr (assoc :noweb params))))
  412. (org-babel-expand-noweb-references info) (nth 1 info))))
  413. (expand-cmd (intern (concat "org-babel-expand-body:" lang)))
  414. (assignments-cmd (intern (concat "org-babel-variable-assignments:"
  415. lang)))
  416. (expanded
  417. (if (fboundp expand-cmd) (funcall expand-cmd body params)
  418. (org-babel-expand-body:generic
  419. body params (and (fboundp assignments-cmd)
  420. (funcall assignments-cmd params))))))
  421. (org-edit-src-code
  422. nil expanded (concat "*Org-Babel Preview " (buffer-name) "[ " lang " ]*"))))
  423. (defun org-babel-edit-distance (s1 s2)
  424. "Return the edit (levenshtein) distance between strings S1 S2."
  425. (let* ((l1 (length s1))
  426. (l2 (length s2))
  427. (dist (map 'vector (lambda (_) (make-vector (1+ l2) nil))
  428. (number-sequence 1 (1+ l1)))))
  429. (flet ((in (i j) (aref (aref dist i) j))
  430. (mmin (&rest lst) (apply #'min (remove nil lst))))
  431. (setf (aref (aref dist 0) 0) 0)
  432. (dolist (i (number-sequence 1 l1))
  433. (dolist (j (number-sequence 1 l2))
  434. (setf (aref (aref dist i) j)
  435. (+ (if (equal (aref s1 (1- i)) (aref s2 (1- j))) 0 1)
  436. (mmin (in (1- i) j) (in i (1- j)) (in (1- i) (1- j)))))))
  437. (in l1 l2))))
  438. ;;;###autoload
  439. (defun org-babel-check-src-block ()
  440. "Check for misspelled header arguments in the current code block."
  441. (interactive)
  442. ;; TODO: report malformed code block
  443. ;; TODO: report incompatible combinations of header arguments
  444. (let ((too-close 2)) ;; <- control closeness to report potential match
  445. (dolist (header (mapcar (lambda (arg) (substring (symbol-name (car arg)) 1))
  446. (and (org-babel-where-is-src-block-head)
  447. (org-babel-parse-header-arguments
  448. (org-babel-clean-text-properties
  449. (match-string 4))))))
  450. (dolist (name (mapcar #'symbol-name org-babel-header-arg-names))
  451. (when (and (not (string= header name))
  452. (<= (org-babel-edit-distance header name) too-close))
  453. (error "supplied header \"%S\" is suspiciously close to \"%S\""
  454. header name))))
  455. (message "No suspicious header arguments found.")))
  456. ;;;###autoload
  457. (defun org-babel-load-in-session (&optional arg info)
  458. "Load the body of the current source-code block.
  459. Evaluate the header arguments for the source block before
  460. entering the session. After loading the body this pops open the
  461. session."
  462. (interactive)
  463. (let* ((info (or info (org-babel-get-src-block-info)))
  464. (lang (nth 0 info))
  465. (params (nth 2 info))
  466. (body (setf (nth 1 info)
  467. (if (and (cdr (assoc :noweb params))
  468. (string= "yes" (cdr (assoc :noweb params))))
  469. (org-babel-expand-noweb-references info)
  470. (nth 1 info))))
  471. (session (cdr (assoc :session params)))
  472. (dir (cdr (assoc :dir params)))
  473. (default-directory
  474. (or (and dir (file-name-as-directory dir)) default-directory))
  475. (cmd (intern (concat "org-babel-load-session:" lang))))
  476. (unless (fboundp cmd)
  477. (error "No org-babel-load-session function for %s!" lang))
  478. (pop-to-buffer (funcall cmd session body params))
  479. (end-of-line 1)))
  480. ;;;###autoload
  481. (defun org-babel-initiate-session (&optional arg info)
  482. "Initiate session for current code block.
  483. If called with a prefix argument then resolve any variable
  484. references in the header arguments and assign these variables in
  485. the session. Copy the body of the code block to the kill ring."
  486. (interactive "P")
  487. (let* ((info (or info (org-babel-get-src-block-info (not arg))))
  488. (lang (nth 0 info))
  489. (body (nth 1 info))
  490. (params (nth 2 info))
  491. (session (cdr (assoc :session params)))
  492. (dir (cdr (assoc :dir params)))
  493. (default-directory
  494. (or (and dir (file-name-as-directory dir)) default-directory))
  495. (init-cmd (intern (format "org-babel-%s-initiate-session" lang)))
  496. (prep-cmd (intern (concat "org-babel-prep-session:" lang))))
  497. (if (and (stringp session) (string= session "none"))
  498. (error "This block is not using a session!"))
  499. (unless (fboundp init-cmd)
  500. (error "No org-babel-initiate-session function for %s!" lang))
  501. (with-temp-buffer (insert (org-babel-trim body))
  502. (copy-region-as-kill (point-min) (point-max)))
  503. (when arg
  504. (unless (fboundp prep-cmd)
  505. (error "No org-babel-prep-session function for %s!" lang))
  506. (funcall prep-cmd session params))
  507. (funcall init-cmd session params)))
  508. ;;;###autoload
  509. (defun org-babel-switch-to-session (&optional arg info)
  510. "Switch to the session of the current code block.
  511. Uses `org-babel-initiate-session' to start the session. If called
  512. with a prefix argument then this is passed on to
  513. `org-babel-initiate-session'."
  514. (interactive "P")
  515. (pop-to-buffer (org-babel-initiate-session arg info))
  516. (end-of-line 1))
  517. (defalias 'org-babel-pop-to-session 'org-babel-switch-to-session)
  518. ;;;###autoload
  519. (defun org-babel-switch-to-session-with-code (&optional arg info)
  520. "Switch to code buffer and display session."
  521. (interactive "P")
  522. (flet ((swap-windows
  523. ()
  524. (let ((other-window-buffer (window-buffer (next-window))))
  525. (set-window-buffer (next-window) (current-buffer))
  526. (set-window-buffer (selected-window) other-window-buffer))
  527. (other-window 1)))
  528. (let ((info (org-babel-get-src-block-info))
  529. (org-src-window-setup 'reorganize-frame))
  530. (save-excursion
  531. (org-babel-switch-to-session arg info))
  532. (org-edit-src-code))
  533. (swap-windows)))
  534. (defmacro org-babel-do-in-edit-buffer (&rest body)
  535. "Evaluate BODY in edit buffer if there is a code block at point.
  536. Return t if a code block was found at point, nil otherwise."
  537. `(let ((org-src-window-setup 'switch-invisibly))
  538. (when (and (org-babel-where-is-src-block-head)
  539. (org-edit-src-code nil nil nil))
  540. (unwind-protect (progn ,@body)
  541. (if (org-bound-and-true-p org-edit-src-from-org-mode)
  542. (org-edit-src-exit)))
  543. t)))
  544. (defun org-babel-do-key-sequence-in-edit-buffer (key)
  545. "Read key sequence and execute the command in edit buffer.
  546. Enter a key sequence to be executed in the language major-mode
  547. edit buffer. For example, TAB will alter the contents of the
  548. Org-mode code block according to the effect of TAB in the
  549. language major-mode buffer. For languages that support
  550. interactive sessions, this can be used to send code from the Org
  551. buffer to the session for evaluation using the native major-mode
  552. evaluation mechanisms."
  553. (interactive "kEnter key-sequence to execute in edit buffer: ")
  554. (org-babel-do-in-edit-buffer
  555. (call-interactively
  556. (key-binding (or key (read-key-sequence nil))))))
  557. (defvar org-bracket-link-regexp)
  558. ;;;###autoload
  559. (defun org-babel-open-src-block-result (&optional re-run)
  560. "If `point' is on a src block then open the results of the
  561. source code block, otherwise return nil. With optional prefix
  562. argument RE-RUN the source-code block is evaluated even if
  563. results already exist."
  564. (interactive "P")
  565. (let ((info (org-babel-get-src-block-info)))
  566. (when info
  567. (save-excursion
  568. ;; go to the results, if there aren't any then run the block
  569. (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
  570. (progn (org-babel-execute-src-block)
  571. (org-babel-where-is-src-block-result))))
  572. (end-of-line 1)
  573. (while (looking-at "[\n\r\t\f ]") (forward-char 1))
  574. ;; open the results
  575. (if (looking-at org-bracket-link-regexp)
  576. ;; file results
  577. (org-open-at-point)
  578. (let ((r (org-babel-format-result
  579. (org-babel-read-result) (cdr (assoc :sep (nth 2 info))))))
  580. (pop-to-buffer (get-buffer-create "*Org-Babel Results*"))
  581. (delete-region (point-min) (point-max))
  582. (insert r)))
  583. t))))
  584. ;;;###autoload
  585. (defmacro org-babel-map-src-blocks (file &rest body)
  586. "Evaluate BODY forms on each source-block in FILE.
  587. If FILE is nil evaluate BODY forms on source blocks in current
  588. buffer. During evaluation of BODY the following local variables
  589. are set relative to the currently matched code block.
  590. full-block ------- string holding the entirety of the code block
  591. beg-block -------- point at the beginning of the code block
  592. end-block -------- point at the end of the matched code block
  593. lang ------------- string holding the language of the code block
  594. beg-lang --------- point at the beginning of the lang
  595. end-lang --------- point at the end of the lang
  596. switches --------- string holding the switches
  597. beg-switches ----- point at the beginning of the switches
  598. end-switches ----- point at the end of the switches
  599. header-args ------ string holding the header-args
  600. beg-header-args -- point at the beginning of the header-args
  601. end-header-args -- point at the end of the header-args
  602. body ------------- string holding the body of the code block
  603. beg-body --------- point at the beginning of the body
  604. end-body --------- point at the end of the body"
  605. (declare (indent 1))
  606. (let ((tempvar (make-symbol "file")))
  607. `(let* ((,tempvar ,file)
  608. (visited-p (or (null ,tempvar)
  609. (get-file-buffer (expand-file-name ,tempvar))))
  610. (point (point)) to-be-removed)
  611. (save-window-excursion
  612. (when ,tempvar (find-file ,tempvar))
  613. (setq to-be-removed (current-buffer))
  614. (goto-char (point-min))
  615. (while (re-search-forward org-babel-src-block-regexp nil t)
  616. (goto-char (match-beginning 0))
  617. (let ((full-block (match-string 0))
  618. (beg-block (match-beginning 0))
  619. (end-block (match-end 0))
  620. (lang (match-string 2))
  621. (beg-lang (match-beginning 2))
  622. (end-lang (match-end 2))
  623. (switches (match-string 3))
  624. (beg-switches (match-beginning 3))
  625. (end-switches (match-end 3))
  626. (header-args (match-string 4))
  627. (beg-header-args (match-beginning 4))
  628. (end-header-args (match-end 4))
  629. (body (match-string 5))
  630. (beg-body (match-beginning 5))
  631. (end-body (match-end 5)))
  632. ,@body
  633. (goto-char end-block))))
  634. (unless visited-p (kill-buffer to-be-removed))
  635. (goto-char point))))
  636. ;;;###autoload
  637. (defmacro org-babel-map-inline-src-blocks (file &rest body)
  638. "Evaluate BODY forms on each inline source-block in FILE.
  639. If FILE is nil evaluate BODY forms on source blocks in current
  640. buffer."
  641. (declare (indent 1))
  642. (let ((tempvar (make-symbol "file")))
  643. `(let* ((,tempvar ,file)
  644. (visited-p (or (null ,tempvar)
  645. (get-file-buffer (expand-file-name ,tempvar))))
  646. (point (point)) to-be-removed)
  647. (save-window-excursion
  648. (when ,tempvar (find-file ,tempvar))
  649. (setq to-be-removed (current-buffer))
  650. (goto-char (point-min))
  651. (while (re-search-forward org-babel-inline-src-block-regexp nil t)
  652. (goto-char (match-beginning 1))
  653. (save-match-data ,@body)
  654. (goto-char (match-end 0))))
  655. (unless visited-p (kill-buffer to-be-removed))
  656. (goto-char point))))
  657. ;;;###autoload
  658. (defun org-babel-execute-buffer (&optional arg)
  659. "Execute source code blocks in a buffer.
  660. Call `org-babel-execute-src-block' on every source block in
  661. the current buffer."
  662. (interactive "P")
  663. (org-babel-eval-wipe-error-buffer)
  664. (org-save-outline-visibility t
  665. (org-babel-map-src-blocks nil
  666. (org-babel-execute-src-block arg))
  667. (org-babel-map-inline-src-blocks nil
  668. (org-babel-execute-src-block arg))))
  669. ;;;###autoload
  670. (defun org-babel-execute-subtree (&optional arg)
  671. "Execute source code blocks in a subtree.
  672. Call `org-babel-execute-src-block' on every source block in
  673. the current subtree."
  674. (interactive "P")
  675. (save-restriction
  676. (save-excursion
  677. (org-narrow-to-subtree)
  678. (org-babel-execute-buffer arg)
  679. (widen))))
  680. ;;;###autoload
  681. (defun org-babel-sha1-hash (&optional info)
  682. "Generate an sha1 hash based on the value of info."
  683. (interactive)
  684. (let ((print-level nil)
  685. (info (or info (org-babel-get-src-block-info))))
  686. (setf (nth 2 info)
  687. (sort (copy-sequence (nth 2 info))
  688. (lambda (a b) (string< (car a) (car b)))))
  689. ((lambda (hash) (when (called-interactively-p 'interactive) (message hash)) hash)
  690. (sha1 (format "%s-%s"
  691. (mapconcat
  692. #'identity
  693. (delq nil
  694. (mapcar
  695. (lambda (arg)
  696. (let ((v (cdr arg)))
  697. (when (and v (not (and (sequencep v)
  698. (not (consp v))
  699. (= (length v) 0))))
  700. ((lambda (el) (format "%S" el))
  701. (cond
  702. ((and (listp v) ; lists are sorted
  703. (member (car arg) '(:result-params)))
  704. (sort (copy-sequence v) #'string<))
  705. ((and (stringp v) ; strings are sorted
  706. (member (car arg) '(:results :exports)))
  707. (mapconcat #'identity (sort (split-string v)
  708. #'string<) " "))
  709. (t v)))))) ; atomic are left untouched
  710. (nth 2 info))) ":")
  711. (nth 1 info))))))
  712. (defun org-babel-result-hash (&optional info)
  713. "Return the in-buffer hash associated with INFO."
  714. (org-babel-where-is-src-block-result nil info)
  715. (org-babel-clean-text-properties (match-string 3)))
  716. (defun org-babel-hide-hash ()
  717. "Hide the hash in the current results line.
  718. Only the initial `org-babel-hash-show' characters of the hash
  719. will remain visible."
  720. (add-to-invisibility-spec '(org-babel-hide-hash . t))
  721. (save-excursion
  722. (when (and (re-search-forward org-babel-result-regexp nil t)
  723. (match-string 3))
  724. (let* ((start (match-beginning 3))
  725. (hide-start (+ org-babel-hash-show start))
  726. (end (match-end 3))
  727. (hash (match-string 3))
  728. ov1 ov2)
  729. (setq ov1 (make-overlay start hide-start))
  730. (setq ov2 (make-overlay hide-start end))
  731. (overlay-put ov2 'invisible 'org-babel-hide-hash)
  732. (overlay-put ov1 'babel-hash hash)))))
  733. (defun org-babel-hide-all-hashes ()
  734. "Hide the hash in the current buffer.
  735. Only the initial `org-babel-hash-show' characters of each hash
  736. will remain visible. This function should be called as part of
  737. the `org-mode-hook'."
  738. (save-excursion
  739. (while (re-search-forward org-babel-result-regexp nil t)
  740. (goto-char (match-beginning 0))
  741. (org-babel-hide-hash)
  742. (goto-char (match-end 0)))))
  743. (add-hook 'org-mode-hook 'org-babel-hide-all-hashes)
  744. (defun org-babel-hash-at-point (&optional point)
  745. "Return the value of the hash at POINT.
  746. The hash is also added as the last element of the kill ring.
  747. This can be called with C-c C-c."
  748. (interactive)
  749. (let ((hash (car (delq nil (mapcar
  750. (lambda (ol) (overlay-get ol 'babel-hash))
  751. (overlays-at (or point (point))))))))
  752. (when hash (kill-new hash) (message hash))))
  753. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
  754. (defun org-babel-result-hide-spec ()
  755. "Hide portions of results lines.
  756. Add `org-babel-hide-result' as an invisibility spec for hiding
  757. portions of results lines."
  758. (add-to-invisibility-spec '(org-babel-hide-result . t)))
  759. (add-hook 'org-mode-hook 'org-babel-result-hide-spec)
  760. (defvar org-babel-hide-result-overlays nil
  761. "Overlays hiding results.")
  762. (defun org-babel-result-hide-all ()
  763. "Fold all results in the current buffer."
  764. (interactive)
  765. (org-babel-show-result-all)
  766. (save-excursion
  767. (while (re-search-forward org-babel-result-regexp nil t)
  768. (save-excursion (goto-char (match-beginning 0))
  769. (org-babel-hide-result-toggle-maybe)))))
  770. (defun org-babel-show-result-all ()
  771. "Unfold all results in the current buffer."
  772. (mapc 'delete-overlay org-babel-hide-result-overlays)
  773. (setq org-babel-hide-result-overlays nil))
  774. ;;;###autoload
  775. (defun org-babel-hide-result-toggle-maybe ()
  776. "Toggle visibility of result at point."
  777. (interactive)
  778. (let ((case-fold-search t))
  779. (if (save-excursion
  780. (beginning-of-line 1)
  781. (looking-at org-babel-result-regexp))
  782. (progn (org-babel-hide-result-toggle)
  783. t) ;; to signal that we took action
  784. nil))) ;; to signal that we did not
  785. (defun org-babel-hide-result-toggle (&optional force)
  786. "Toggle the visibility of the current result."
  787. (interactive)
  788. (save-excursion
  789. (beginning-of-line)
  790. (if (re-search-forward org-babel-result-regexp nil t)
  791. (let ((start (progn (beginning-of-line 2) (- (point) 1)))
  792. (end (progn (goto-char (- (org-babel-result-end) 1)) (point)))
  793. ov)
  794. (if (memq t (mapcar (lambda (overlay)
  795. (eq (overlay-get overlay 'invisible)
  796. 'org-babel-hide-result))
  797. (overlays-at start)))
  798. (if (or (not force) (eq force 'off))
  799. (mapc (lambda (ov)
  800. (when (member ov org-babel-hide-result-overlays)
  801. (setq org-babel-hide-result-overlays
  802. (delq ov org-babel-hide-result-overlays)))
  803. (when (eq (overlay-get ov 'invisible)
  804. 'org-babel-hide-result)
  805. (delete-overlay ov)))
  806. (overlays-at start)))
  807. (setq ov (make-overlay start end))
  808. (overlay-put ov 'invisible 'org-babel-hide-result)
  809. ;; make the block accessible to isearch
  810. (overlay-put
  811. ov 'isearch-open-invisible
  812. (lambda (ov)
  813. (when (member ov org-babel-hide-result-overlays)
  814. (setq org-babel-hide-result-overlays
  815. (delq ov org-babel-hide-result-overlays)))
  816. (when (eq (overlay-get ov 'invisible)
  817. 'org-babel-hide-result)
  818. (delete-overlay ov))))
  819. (push ov org-babel-hide-result-overlays)))
  820. (error "Not looking at a result line"))))
  821. ;; org-tab-after-check-for-cycling-hook
  822. (add-hook 'org-tab-first-hook 'org-babel-hide-result-toggle-maybe)
  823. ;; Remove overlays when changing major mode
  824. (add-hook 'org-mode-hook
  825. (lambda () (org-add-hook 'change-major-mode-hook
  826. 'org-babel-show-result-all 'append 'local)))
  827. (defvar org-file-properties)
  828. (defun org-babel-params-from-properties (&optional lang)
  829. "Retrieve parameters specified as properties.
  830. Return an association list of any source block params which
  831. may be specified in the properties of the current outline entry."
  832. (save-match-data
  833. (let (val sym)
  834. (delq nil
  835. (mapcar
  836. (lambda (header-arg)
  837. (and (setq val
  838. (or (org-entry-get (point) header-arg t)
  839. (org-entry-get (point) (concat ":" header-arg) t)))
  840. (cons (intern (concat ":" header-arg))
  841. (org-babel-read val))))
  842. (mapcar
  843. 'symbol-name
  844. (append
  845. org-babel-header-arg-names
  846. (progn
  847. (setq sym (intern (concat "org-babel-header-arg-names:" lang)))
  848. (and (boundp sym) (eval sym))))))))))
  849. (defun org-babel-params-from-buffer ()
  850. "Retrieve per-buffer parameters.
  851. Return an association list of any source block params which
  852. may be specified in the current buffer."
  853. (let (local-properties)
  854. (save-match-data
  855. (save-excursion
  856. (save-restriction
  857. (widen)
  858. (goto-char (point-min))
  859. (while (re-search-forward
  860. (org-make-options-regexp (list "BABEL" "PROPERTIES")) nil t)
  861. (setq local-properties
  862. (org-babel-merge-params
  863. local-properties
  864. (org-babel-parse-header-arguments
  865. (org-match-string-no-properties 2)))))
  866. local-properties)))))
  867. (defvar org-src-preserve-indentation)
  868. (defun org-babel-parse-src-block-match ()
  869. "Parse the results from a match of the `org-babel-src-block-regexp'."
  870. (let* ((block-indentation (length (match-string 1)))
  871. (lang (org-babel-clean-text-properties (match-string 2)))
  872. (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
  873. (switches (match-string 3))
  874. (body (org-babel-clean-text-properties (match-string 5)))
  875. (preserve-indentation (or org-src-preserve-indentation
  876. (string-match "-i\\>" switches))))
  877. (list lang
  878. ;; get block body less properties, protective commas, and indentation
  879. (with-temp-buffer
  880. (save-match-data
  881. (insert (org-babel-strip-protective-commas body))
  882. (unless preserve-indentation (org-do-remove-indentation))
  883. (buffer-string)))
  884. (org-babel-merge-params
  885. org-babel-default-header-args
  886. (org-babel-params-from-buffer)
  887. (org-babel-params-from-properties lang)
  888. (if (boundp lang-headers) (eval lang-headers) nil)
  889. (org-babel-parse-header-arguments
  890. (org-babel-clean-text-properties (or (match-string 4) ""))))
  891. switches
  892. block-indentation)))
  893. (defun org-babel-parse-inline-src-block-match ()
  894. "Parse the results from a match of the `org-babel-inline-src-block-regexp'."
  895. (let* ((lang (org-babel-clean-text-properties (match-string 2)))
  896. (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
  897. (list lang
  898. (org-babel-strip-protective-commas
  899. (org-babel-clean-text-properties (match-string 5)))
  900. (org-babel-merge-params
  901. org-babel-default-inline-header-args
  902. (org-babel-params-from-buffer)
  903. (org-babel-params-from-properties lang)
  904. (if (boundp lang-headers) (eval lang-headers) nil)
  905. (org-babel-parse-header-arguments
  906. (org-babel-clean-text-properties (or (match-string 4) "")))))))
  907. (defun org-babel-parse-header-arguments (arg-string)
  908. "Parse a string of header arguments returning an alist."
  909. (when (> (length arg-string) 0)
  910. (delq nil
  911. (mapcar
  912. (lambda (arg)
  913. (if (string-match
  914. "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)"
  915. arg)
  916. (cons (intern (match-string 1 arg))
  917. (org-babel-read (org-babel-chomp (match-string 2 arg))))
  918. (cons (intern (org-babel-chomp arg)) nil)))
  919. (let ((balance 0) (partial nil) (lst nil) (last 0))
  920. (mapc (lambda (ch) ; split on [] balanced instances of [ \t]:
  921. (setq balance (+ balance
  922. (cond ((equal 91 ch) 1)
  923. ((equal 93 ch) -1)
  924. (t 0))))
  925. (setq partial (cons ch partial))
  926. (when (and (= ch 58) (= balance 0)
  927. (or (= last 32) (= last 9)))
  928. (setq lst (cons (apply #'string (nreverse (cddr partial)))
  929. lst))
  930. (setq partial (list ch)))
  931. (setq last ch))
  932. (string-to-list arg-string))
  933. (nreverse (cons (apply #'string (nreverse partial)) lst)))))))
  934. (defun org-babel-process-params (params)
  935. "Expand variables in PARAMS and add summary parameters."
  936. (let* ((vars-and-names (org-babel-disassemble-tables
  937. (mapcar (lambda (el)
  938. (if (consp (cdr el))
  939. (cdr el) (org-babel-ref-parse (cdr el))))
  940. (org-babel-get-header params :var))
  941. (cdr (assoc :hlines params))
  942. (cdr (assoc :colnames params))
  943. (cdr (assoc :rownames params))))
  944. (raw-result (or (cdr (assoc :results params)) ""))
  945. (result-params (append
  946. (split-string (if (stringp raw-result)
  947. raw-result
  948. (eval raw-result)))
  949. (cdr (assoc :result-params params)))))
  950. (append
  951. (mapcar (lambda (var) (cons :var var)) (car vars-and-names))
  952. (list
  953. (cons :colname-names (or (cdr (assoc :colname-names params))
  954. (cadr vars-and-names)))
  955. (cons :rowname-names (or (cdr (assoc :rowname-names params))
  956. (caddr vars-and-names)))
  957. (cons :result-params result-params)
  958. (cons :result-type (cond ((member "output" result-params) 'output)
  959. ((member "value" result-params) 'value)
  960. (t 'value))))
  961. (org-babel-get-header params :var 'other))))
  962. ;; row and column names
  963. (defun org-babel-del-hlines (table)
  964. "Remove all 'hlines from TABLE."
  965. (remove 'hline table))
  966. (defun org-babel-get-colnames (table)
  967. "Return the column names of TABLE.
  968. Return a cons cell, the `car' of which contains the TABLE less
  969. colnames, and the `cdr' of which contains a list of the column
  970. names."
  971. (if (equal 'hline (nth 1 table))
  972. (cons (cddr table) (car table))
  973. (cons (cdr table) (car table))))
  974. (defun org-babel-get-rownames (table)
  975. "Return the row names of TABLE.
  976. Return a cons cell, the `car' of which contains the TABLE less
  977. colnames, and the `cdr' of which contains a list of the column
  978. names. Note: this function removes any hlines in TABLE."
  979. (flet ((trans (table) (apply #'mapcar* #'list table)))
  980. (let* ((width (apply 'max
  981. (mapcar (lambda (el) (if (listp el) (length el) 0)) table)))
  982. (table (trans (mapcar (lambda (row)
  983. (if (not (equal row 'hline))
  984. row
  985. (setq row '())
  986. (dotimes (n width)
  987. (setq row (cons 'hline row)))
  988. row))
  989. table))))
  990. (cons (mapcar (lambda (row) (if (equal (car row) 'hline) 'hline row))
  991. (trans (cdr table)))
  992. (remove 'hline (car table))))))
  993. (defun org-babel-put-colnames (table colnames)
  994. "Add COLNAMES to TABLE if they exist."
  995. (if colnames (apply 'list colnames 'hline table) table))
  996. (defun org-babel-put-rownames (table rownames)
  997. "Add ROWNAMES to TABLE if they exist."
  998. (if rownames
  999. (mapcar (lambda (row)
  1000. (if (listp row)
  1001. (cons (or (pop rownames) "") row)
  1002. row)) table)
  1003. table))
  1004. (defun org-babel-pick-name (names selector)
  1005. "Select one out of an alist of row or column names.
  1006. SELECTOR can be either a list of names in which case those names
  1007. will be returned directly, or an index into the list NAMES in
  1008. which case the indexed names will be return."
  1009. (if (listp selector)
  1010. selector
  1011. (when names
  1012. (if (and selector (symbolp selector) (not (equal t selector)))
  1013. (cdr (assoc selector names))
  1014. (if (integerp selector)
  1015. (nth (- selector 1) names)
  1016. (cdr (car (last names))))))))
  1017. (defun org-babel-disassemble-tables (vars hlines colnames rownames)
  1018. "Parse tables for further processing.
  1019. Process the variables in VARS according to the HLINES,
  1020. ROWNAMES and COLNAMES header arguments. Return a list consisting
  1021. of the vars, cnames and rnames."
  1022. (let (cnames rnames)
  1023. (list
  1024. (mapcar
  1025. (lambda (var)
  1026. (when (listp (cdr var))
  1027. (when (and (not (equal colnames "no"))
  1028. (or colnames (and (equal (nth 1 (cdr var)) 'hline)
  1029. (not (member 'hline (cddr (cdr var)))))))
  1030. (let ((both (org-babel-get-colnames (cdr var))))
  1031. (setq cnames (cons (cons (car var) (cdr both))
  1032. cnames))
  1033. (setq var (cons (car var) (car both)))))
  1034. (when (and rownames (not (equal rownames "no")))
  1035. (let ((both (org-babel-get-rownames (cdr var))))
  1036. (setq rnames (cons (cons (car var) (cdr both))
  1037. rnames))
  1038. (setq var (cons (car var) (car both)))))
  1039. (when (and hlines (not (equal hlines "yes")))
  1040. (setq var (cons (car var) (org-babel-del-hlines (cdr var))))))
  1041. var)
  1042. vars)
  1043. cnames rnames)))
  1044. (defun org-babel-reassemble-table (table colnames rownames)
  1045. "Add column and row names to a table.
  1046. Given a TABLE and set of COLNAMES and ROWNAMES add the names
  1047. to the table for reinsertion to org-mode."
  1048. (if (listp table)
  1049. ((lambda (table)
  1050. (if (and colnames (listp (car table)) (= (length (car table))
  1051. (length colnames)))
  1052. (org-babel-put-colnames table colnames) table))
  1053. (if (and rownames (= (length table) (length rownames)))
  1054. (org-babel-put-rownames table rownames) table))
  1055. table))
  1056. (defun org-babel-where-is-src-block-head ()
  1057. "Find where the current source block begins.
  1058. Return the point at the beginning of the current source
  1059. block. Specifically at the beginning of the #+BEGIN_SRC line.
  1060. If the point is not on a source block then return nil."
  1061. (let ((initial (point)) top bottom)
  1062. (or
  1063. (save-excursion ;; on a source name line or a #+header line
  1064. (beginning-of-line 1)
  1065. (and (or (looking-at org-babel-src-name-regexp)
  1066. (looking-at org-babel-multi-line-header-regexp))
  1067. (progn
  1068. (while (and (forward-line 1)
  1069. (looking-at org-babel-multi-line-header-regexp)))
  1070. (looking-at org-babel-src-block-regexp))
  1071. (point)))
  1072. (save-excursion ;; on a #+begin_src line
  1073. (beginning-of-line 1)
  1074. (and (looking-at org-babel-src-block-regexp)
  1075. (point)))
  1076. (save-excursion ;; inside a src block
  1077. (and
  1078. (re-search-backward "^[ \t]*#\\+begin_src" nil t) (setq top (point))
  1079. (re-search-forward "^[ \t]*#\\+end_src" nil t) (setq bottom (point))
  1080. (< top initial) (< initial bottom)
  1081. (progn (goto-char top) (beginning-of-line 1)
  1082. (looking-at org-babel-src-block-regexp))
  1083. (point))))))
  1084. ;;;###autoload
  1085. (defun org-babel-goto-src-block-head ()
  1086. "Go to the beginning of the current code block."
  1087. (interactive)
  1088. ((lambda (head)
  1089. (if head (goto-char head) (error "not currently in a code block")))
  1090. (org-babel-where-is-src-block-head)))
  1091. ;;;###autoload
  1092. (defun org-babel-goto-named-src-block (name)
  1093. "Go to a named source-code block."
  1094. (interactive
  1095. (let ((completion-ignore-case t))
  1096. (list (org-icompleting-read "source-block name: "
  1097. (org-babel-src-block-names) nil t))))
  1098. (let ((point (org-babel-find-named-block name)))
  1099. (if point
  1100. ;; taken from `org-open-at-point'
  1101. (progn (goto-char point) (org-show-context))
  1102. (message "source-code block '%s' not found in this buffer" name))))
  1103. (defun org-babel-find-named-block (name)
  1104. "Find a named source-code block.
  1105. Return the location of the source block identified by source
  1106. NAME, or nil if no such block exists. Set match data according to
  1107. org-babel-named-src-block-regexp."
  1108. (save-excursion
  1109. (let ((case-fold-search t)
  1110. (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
  1111. (goto-char (point-min))
  1112. (when (or (re-search-forward regexp nil t)
  1113. (re-search-backward regexp nil t))
  1114. (match-beginning 0)))))
  1115. (defun org-babel-src-block-names (&optional file)
  1116. "Returns the names of source blocks in FILE or the current buffer."
  1117. (save-excursion
  1118. (when file (find-file file)) (goto-char (point-min))
  1119. (let (names)
  1120. (while (re-search-forward org-babel-src-name-w-name-regexp nil t)
  1121. (setq names (cons (match-string 4) names)))
  1122. names)))
  1123. ;;;###autoload
  1124. (defun org-babel-goto-named-result (name)
  1125. "Go to a named result."
  1126. (interactive
  1127. (let ((completion-ignore-case t))
  1128. (list (org-icompleting-read "source-block name: "
  1129. (org-babel-result-names) nil t))))
  1130. (let ((point (org-babel-find-named-result name)))
  1131. (if point
  1132. ;; taken from `org-open-at-point'
  1133. (progn (goto-char point) (org-show-context))
  1134. (message "result '%s' not found in this buffer" name))))
  1135. (defun org-babel-find-named-result (name)
  1136. "Find a named result.
  1137. Return the location of the result named NAME in the current
  1138. buffer or nil if no such result exists."
  1139. (save-excursion
  1140. (goto-char (point-min))
  1141. (when (re-search-forward
  1142. (concat org-babel-result-regexp
  1143. "[ \t]" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
  1144. (beginning-of-line 0) (point))))
  1145. (defun org-babel-result-names (&optional file)
  1146. "Returns the names of results in FILE or the current buffer."
  1147. (save-excursion
  1148. (when file (find-file file)) (goto-char (point-min))
  1149. (let (names)
  1150. (while (re-search-forward org-babel-result-w-name-regexp nil t)
  1151. (setq names (cons (match-string 4) names)))
  1152. names)))
  1153. ;;;###autoload
  1154. (defun org-babel-next-src-block (&optional arg)
  1155. "Jump to the next source block.
  1156. With optional prefix argument ARG, jump forward ARG many source blocks."
  1157. (interactive "P")
  1158. (when (looking-at org-babel-src-block-regexp) (forward-char 1))
  1159. (condition-case nil
  1160. (re-search-forward org-babel-src-block-regexp nil nil (or arg 1))
  1161. (error (error "No further code blocks")))
  1162. (goto-char (match-beginning 0)) (org-show-context))
  1163. ;;;###autoload
  1164. (defun org-babel-previous-src-block (&optional arg)
  1165. "Jump to the previous source block.
  1166. With optional prefix argument ARG, jump backward ARG many source blocks."
  1167. (interactive "P")
  1168. (condition-case nil
  1169. (re-search-backward org-babel-src-block-regexp nil nil (or arg 1))
  1170. (error (error "No previous code blocks")))
  1171. (goto-char (match-beginning 0)) (org-show-context))
  1172. (defvar org-babel-load-languages)
  1173. ;;;###autoload
  1174. (defun org-babel-mark-block ()
  1175. "Mark current src block"
  1176. (interactive)
  1177. ((lambda (head)
  1178. (when head
  1179. (save-excursion
  1180. (goto-char head)
  1181. (looking-at org-babel-src-block-regexp))
  1182. (push-mark (match-end 5) nil t)
  1183. (goto-char (match-beginning 5))))
  1184. (org-babel-where-is-src-block-head)))
  1185. (defun org-babel-demarcate-block (&optional arg)
  1186. "Wrap or split the code in the region or on the point.
  1187. When called from inside of a code block the current block is
  1188. split. When called from outside of a code block a new code block
  1189. is created. In both cases if the region is demarcated and if the
  1190. region is not active then the point is demarcated."
  1191. (interactive "P")
  1192. (let ((info (org-babel-get-src-block-info 'light))
  1193. (stars (concat (make-string (or (org-current-level) 1) ?*) " ")))
  1194. (if info
  1195. (mapc
  1196. (lambda (place)
  1197. (save-excursion
  1198. (goto-char place)
  1199. (let ((lang (nth 0 info))
  1200. (indent (make-string (nth 5 info) ? )))
  1201. (when (string-match "^[[:space:]]*$"
  1202. (buffer-substring (point-at-bol)
  1203. (point-at-eol)))
  1204. (delete-region (point-at-bol) (point-at-eol)))
  1205. (insert (concat (if (looking-at "^") "" "\n")
  1206. indent "#+end_src\n"
  1207. (if arg stars indent) "\n"
  1208. indent "#+begin_src " lang
  1209. (if (looking-at "[\n\r]") "" "\n")))))
  1210. (move-end-of-line 2))
  1211. (sort (if (region-active-p) (list (mark) (point)) (list (point))) #'>))
  1212. (let ((start (point))
  1213. (lang (org-icompleting-read "Lang: "
  1214. (mapcar (lambda (el) (symbol-name (car el)))
  1215. org-babel-load-languages)))
  1216. (body (delete-and-extract-region
  1217. (if (region-active-p) (mark) (point)) (point))))
  1218. (insert (concat (if (looking-at "^") "" "\n")
  1219. (if arg (concat stars "\n") "")
  1220. "#+begin_src " lang "\n"
  1221. body
  1222. (if (or (= (length body) 0)
  1223. (string-match "[\r\n]$" body)) "" "\n")
  1224. "#+end_src\n"))
  1225. (goto-char start) (move-end-of-line 1)))))
  1226. (defvar org-babel-lob-one-liner-regexp)
  1227. (defun org-babel-where-is-src-block-result (&optional insert info hash indent)
  1228. "Find where the current source block results begin.
  1229. Return the point at the beginning of the result of the current
  1230. source block. Specifically at the beginning of the results line.
  1231. If no result exists for this block then create a results line
  1232. following the source block."
  1233. (save-excursion
  1234. (let* ((on-lob-line (save-excursion
  1235. (beginning-of-line 1)
  1236. (looking-at org-babel-lob-one-liner-regexp)))
  1237. (inlinep (save-excursion
  1238. (re-search-backward "[ \f\t\n\r\v]" nil t)
  1239. (when (looking-at org-babel-inline-src-block-regexp)
  1240. (match-end 0))))
  1241. (name (if on-lob-line
  1242. (nth 0 (org-babel-lob-get-info))
  1243. (nth 4 (or info (org-babel-get-src-block-info)))))
  1244. (head (unless on-lob-line (org-babel-where-is-src-block-head)))
  1245. found beg end)
  1246. (when head (goto-char head))
  1247. (setq
  1248. found ;; was there a result (before we potentially insert one)
  1249. (or
  1250. inlinep
  1251. (and
  1252. ;; named results:
  1253. ;; - return t if it is found, else return nil
  1254. ;; - if it does not need to be rebuilt, then don't set end
  1255. ;; - if it does need to be rebuilt then do set end
  1256. name (setq beg (org-babel-find-named-result name))
  1257. (prog1 beg
  1258. (when (and hash (not (string= hash (match-string 3))))
  1259. (goto-char beg) (setq end beg) ;; beginning of result
  1260. (forward-line 1)
  1261. (delete-region end (org-babel-result-end)) nil)))
  1262. (and
  1263. ;; unnamed results:
  1264. ;; - return t if it is found, else return nil
  1265. ;; - if it is found, and the hash doesn't match, delete and set end
  1266. (or on-lob-line (re-search-forward "^[ \t]*#\\+end_src" nil t))
  1267. (progn (end-of-line 1)
  1268. (if (eobp) (insert "\n") (forward-char 1))
  1269. (setq end (point))
  1270. (or (and (not name)
  1271. (progn ;; unnamed results line already exists
  1272. (re-search-forward "[^ \f\t\n\r\v]" nil t)
  1273. (beginning-of-line 1)
  1274. (looking-at
  1275. (concat org-babel-result-regexp "\n")))
  1276. (prog1 (point)
  1277. ;; must remove and rebuild if hash!=old-hash
  1278. (if (and hash (not (string= hash (match-string 3))))
  1279. (prog1 nil
  1280. (forward-line 1)
  1281. (delete-region
  1282. end (org-babel-result-end)))
  1283. (setq end nil)))))))))
  1284. (if (and insert end)
  1285. (progn
  1286. (goto-char end)
  1287. (unless beg
  1288. (if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
  1289. (insert (concat
  1290. (if indent
  1291. (mapconcat
  1292. (lambda (el) " ")
  1293. (org-number-sequence 1 indent) "")
  1294. "")
  1295. "#+results"
  1296. (when hash (concat "["hash"]"))
  1297. ":"
  1298. (when name (concat " " name)) "\n"))
  1299. (unless beg (insert "\n") (backward-char))
  1300. (beginning-of-line 0)
  1301. (if hash (org-babel-hide-hash))
  1302. (point))
  1303. found))))
  1304. (defvar org-block-regexp)
  1305. (defun org-babel-read-result ()
  1306. "Read the result at `point' into emacs-lisp."
  1307. (let ((case-fold-search t) result-string)
  1308. (cond
  1309. ((org-at-table-p) (org-babel-read-table))
  1310. ((org-at-item-p) (org-babel-read-list))
  1311. ((looking-at org-bracket-link-regexp) (org-babel-read-link))
  1312. ((looking-at org-block-regexp) (org-babel-trim (match-string 4)))
  1313. ((looking-at "^[ \t]*: ")
  1314. (setq result-string
  1315. (org-babel-trim
  1316. (mapconcat (lambda (line)
  1317. (if (and (> (length line) 1)
  1318. (string-match "^[ \t]*: \\(.+\\)" line))
  1319. (match-string 1 line)
  1320. line))
  1321. (split-string
  1322. (buffer-substring
  1323. (point) (org-babel-result-end)) "[\r\n]+")
  1324. "\n")))
  1325. (or (org-babel-number-p result-string) result-string))
  1326. ((looking-at org-babel-result-regexp)
  1327. (save-excursion (forward-line 1) (org-babel-read-result))))))
  1328. (defun org-babel-read-table ()
  1329. "Read the table at `point' into emacs-lisp."
  1330. (mapcar (lambda (row)
  1331. (if (and (symbolp row) (equal row 'hline)) row
  1332. (mapcar (lambda (el) (org-babel-read el 'inhibit-lisp-eval)) row)))
  1333. (org-table-to-lisp)))
  1334. (defun org-babel-read-list ()
  1335. "Read the list at `point' into emacs-lisp."
  1336. (mapcar (lambda (el) (org-babel-read el 'inhibit-lisp-eval))
  1337. (mapcar #'cadr (cdr (org-list-parse-list)))))
  1338. (defvar org-link-types-re)
  1339. (defun org-babel-read-link ()
  1340. "Read the link at `point' into emacs-lisp.
  1341. If the path of the link is a file path it is expanded using
  1342. `expand-file-name'."
  1343. (let* ((case-fold-search t)
  1344. (raw (and (looking-at org-bracket-link-regexp)
  1345. (org-babel-clean-text-properties (match-string 1))))
  1346. (type (and (string-match org-link-types-re raw)
  1347. (match-string 1 raw))))
  1348. (cond
  1349. ((not type) (expand-file-name raw))
  1350. ((string= type "file")
  1351. (and (string-match "file\\(.*\\):\\(.+\\)" raw)
  1352. (expand-file-name (match-string 2 raw))))
  1353. (t raw))))
  1354. (defun org-babel-format-result (result &optional sep)
  1355. "Format RESULT for writing to file."
  1356. (flet ((echo-res (result)
  1357. (if (stringp result) result (format "%S" result))))
  1358. (if (listp result)
  1359. ;; table result
  1360. (orgtbl-to-generic
  1361. result
  1362. (list
  1363. :sep (or sep "\t")
  1364. :fmt 'echo-res))
  1365. ;; scalar result
  1366. (echo-res result))))
  1367. (defun org-babel-insert-result
  1368. (result &optional result-params info hash indent lang)
  1369. "Insert RESULT into the current buffer.
  1370. By default RESULT is inserted after the end of the
  1371. current source block. With optional argument RESULT-PARAMS
  1372. controls insertion of results in the org-mode file.
  1373. RESULT-PARAMS can take the following values...
  1374. replace - (default option) insert results after the source block
  1375. replacing any previously inserted results
  1376. silent -- no results are inserted
  1377. file ---- the results are interpreted as a file path, and are
  1378. inserted into the buffer using the Org-mode file syntax
  1379. list ---- the results are interpreted as an Org-mode list.
  1380. raw ----- results are added directly to the Org-mode file. This
  1381. is a good option if you code block will output org-mode
  1382. formatted text.
  1383. org ----- similar in effect to raw, only the results are wrapped
  1384. in an org code block. Similar to the raw option, on
  1385. export the results will be interpreted as org-formatted
  1386. text, however by wrapping the results in an org code
  1387. block they can be replaced upon re-execution of the
  1388. code block.
  1389. html ---- results are added inside of a #+BEGIN_HTML block. This
  1390. is a good option if you code block will output html
  1391. formatted text.
  1392. latex --- results are added inside of a #+BEGIN_LATEX block.
  1393. This is a good option if you code block will output
  1394. latex formatted text.
  1395. code ---- the results are extracted in the syntax of the source
  1396. code of the language being evaluated and are added
  1397. inside of a #+BEGIN_SRC block with the source-code
  1398. language set appropriately. Note this relies on the
  1399. optional LANG argument."
  1400. (if (stringp result)
  1401. (progn
  1402. (setq result (org-babel-clean-text-properties result))
  1403. (when (member "file" result-params)
  1404. (setq result (org-babel-result-to-file result))))
  1405. (unless (listp result) (setq result (format "%S" result))))
  1406. (if (and result-params (member "silent" result-params))
  1407. (progn
  1408. (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
  1409. result)
  1410. (save-excursion
  1411. (let* ((inlinep (save-excursion
  1412. (re-search-backward "[ \f\t\n\r\v]" nil t)
  1413. (when (looking-at org-babel-inline-src-block-regexp)
  1414. (goto-char (match-end 0))
  1415. (insert (if (listp result) "\n" " "))
  1416. (point))))
  1417. (existing-result (unless inlinep
  1418. (org-babel-where-is-src-block-result
  1419. t info hash indent)))
  1420. (results-switches
  1421. (cdr (assoc :results_switches (nth 2 info))))
  1422. beg end)
  1423. (when (and (stringp result) ; ensure results end in a newline
  1424. (not inlinep)
  1425. (> (length result) 0)
  1426. (not (or (string-equal (substring result -1) "\n")
  1427. (string-equal (substring result -1) "\r"))))
  1428. (setq result (concat result "\n")))
  1429. (if (not existing-result)
  1430. (setq beg (or inlinep (point)))
  1431. (goto-char existing-result)
  1432. (save-excursion
  1433. (re-search-forward "#" nil t)
  1434. (setq indent (- (current-column) 1)))
  1435. (forward-line 1)
  1436. (setq beg (point))
  1437. (cond
  1438. ((member "replace" result-params)
  1439. (delete-region (point) (org-babel-result-end)))
  1440. ((member "append" result-params)
  1441. (goto-char (org-babel-result-end)) (setq beg (point-marker)))
  1442. ((member "prepend" result-params)))) ; already there
  1443. (setq results-switches
  1444. (if results-switches (concat " " results-switches) ""))
  1445. ;; insert results based on type
  1446. (cond
  1447. ;; do nothing for an empty result
  1448. ((= (length result) 0))
  1449. ;; insert a list if preferred
  1450. ((member "list" result-params)
  1451. (insert
  1452. (org-babel-trim
  1453. (org-list-to-generic
  1454. (cons 'unordered
  1455. (mapcar
  1456. (lambda (el) (list nil (if (stringp el) el (format "%S" el))))
  1457. (if (listp result) result (list result))))
  1458. '(:splicep nil :istart "- " :iend "\n")))
  1459. "\n"))
  1460. ;; assume the result is a table if it's not a string
  1461. ((not (stringp result))
  1462. (goto-char beg)
  1463. (insert (concat (orgtbl-to-orgtbl
  1464. (if (or (eq 'hline (car result))
  1465. (and (listp (car result))
  1466. (listp (cdr (car result)))))
  1467. result (list result))
  1468. '(:fmt (lambda (cell) (format "%s" cell)))) "\n"))
  1469. (goto-char beg) (when (org-at-table-p) (org-table-align)))
  1470. ((member "file" result-params)
  1471. (insert result))
  1472. (t (goto-char beg) (insert result)))
  1473. (when (listp result) (goto-char (org-table-end)))
  1474. (setq end (point-marker))
  1475. ;; possibly wrap result
  1476. (flet ((wrap (start finish)
  1477. (goto-char beg) (insert (concat start "\n"))
  1478. (goto-char end) (insert (concat finish "\n"))
  1479. (setq end (point-marker))))
  1480. (cond
  1481. ((member "html" result-params)
  1482. (wrap "#+BEGIN_HTML" "#+END_HTML"))
  1483. ((member "latex" result-params)
  1484. (wrap "#+BEGIN_LaTeX" "#+END_LaTeX"))
  1485. ((member "code" result-params)
  1486. (wrap (format "#+BEGIN_SRC %s%s" (or lang "none") results-switches)
  1487. "#+END_SRC"))
  1488. ((member "org" result-params)
  1489. (wrap "#+BEGIN_ORG" "#+END_ORG"))
  1490. ((member "raw" result-params)
  1491. (goto-char beg) (if (org-at-table-p) (org-cycle)))
  1492. ((member "wrap" result-params)
  1493. (when (and (stringp result) (not (member "file" result-params)))
  1494. (org-babel-examplize-region beg end results-switches))
  1495. (wrap "#+BEGIN_RESULT" "#+END_RESULT"))
  1496. ((and (stringp result) (not (member "file" result-params)))
  1497. (org-babel-examplize-region beg end results-switches)
  1498. (setq end (point)))))
  1499. ;; possibly indent the results to match the #+results line
  1500. (when (and (not inlinep) (numberp indent) indent (> indent 0)
  1501. ;; in this case `table-align' does the work for us
  1502. (not (and (listp result)
  1503. (member "append" result-params))))
  1504. (indent-rigidly beg end indent))))
  1505. (if (= (length result) 0)
  1506. (if (member "value" result-params)
  1507. (message "Code block returned no value.")
  1508. (message "Code block produced no output."))
  1509. (message "Code block evaluation complete."))))
  1510. (defun org-babel-remove-result (&optional info)
  1511. "Remove the result of the current source block."
  1512. (interactive)
  1513. (let ((location (org-babel-where-is-src-block-result nil info)) start)
  1514. (when location
  1515. (save-excursion
  1516. (goto-char location) (setq start (point)) (forward-line 1)
  1517. (delete-region start (org-babel-result-end))))))
  1518. (defun org-babel-result-end ()
  1519. "Return the point at the end of the current set of results"
  1520. (save-excursion
  1521. (cond
  1522. ((org-at-table-p) (progn (goto-char (org-table-end)) (point)))
  1523. ((org-at-item-p) (let* ((struct (org-list-struct))
  1524. (prvs (org-list-prevs-alist struct)))
  1525. (org-list-get-list-end (point-at-bol) struct prvs)))
  1526. (t
  1527. (let ((case-fold-search t)
  1528. (blocks-re (regexp-opt
  1529. (list "latex" "html" "example" "src" "result" "org"))))
  1530. (if (looking-at (concat "[ \t]*#\\+begin_" blocks-re))
  1531. (progn (re-search-forward (concat "[ \t]*#\\+end_" blocks-re) nil t)
  1532. (forward-char 1))
  1533. (while (looking-at "[ \t]*\\(: \\|\\[\\[\\)")
  1534. (forward-line 1))))
  1535. (point)))))
  1536. (defun org-babel-result-to-file (result)
  1537. "Convert RESULT into an `org-mode' link.
  1538. If the `default-directory' is different from the containing
  1539. file's directory then expand relative links."
  1540. (flet ((cond-exp (file)
  1541. (if (and default-directory
  1542. buffer-file-name
  1543. (not (string= (expand-file-name default-directory)
  1544. (expand-file-name
  1545. (file-name-directory buffer-file-name)))))
  1546. (expand-file-name file default-directory)
  1547. file)))
  1548. (if (stringp result)
  1549. (format "[[file:%s]]" (cond-exp result))
  1550. (when (and (listp result) (= 2 (length result))
  1551. (stringp (car result)) (stringp (cadr result)))
  1552. (format "[[file:%s][%s]]" (car result) (cadr result))))))
  1553. (defun org-babel-examplize-region (beg end &optional results-switches)
  1554. "Comment out region using the inline '==' or ': ' org example quote."
  1555. (interactive "*r")
  1556. (flet ((chars-between (b e) (string-match "[\\S]" (buffer-substring b e))))
  1557. (if (or (chars-between (save-excursion (goto-char beg) (point-at-bol)) beg)
  1558. (chars-between end (save-excursion (goto-char end) (point-at-eol))))
  1559. (save-excursion
  1560. (goto-char beg)
  1561. (insert (format "=%s=" (prog1 (buffer-substring beg end)
  1562. (delete-region beg end)))))
  1563. (let ((size (count-lines beg end)))
  1564. (save-excursion
  1565. (cond ((= size 0)) ; do nothing for an empty result
  1566. ((< size org-babel-min-lines-for-block-output)
  1567. (goto-char beg)
  1568. (dotimes (n size)
  1569. (beginning-of-line 1) (insert ": ") (forward-line 1)))
  1570. (t
  1571. (goto-char beg)
  1572. (insert (if results-switches
  1573. (format "#+begin_example%s\n" results-switches)
  1574. "#+begin_example\n"))
  1575. (if (markerp end) (goto-char end) (forward-char (- end beg)))
  1576. (insert "#+end_example\n"))))))))
  1577. (defun org-babel-update-block-body (new-body)
  1578. "Update the body of the current code block to NEW-BODY."
  1579. (if (not (org-babel-where-is-src-block-head))
  1580. (error "not in source block")
  1581. (save-match-data
  1582. (replace-match (concat (org-babel-trim new-body) "\n") nil t nil 5))
  1583. (indent-rigidly (match-beginning 5) (match-end 5) 2)))
  1584. (defun org-babel-merge-params (&rest plists)
  1585. "Combine all parameter association lists in PLISTS.
  1586. Later elements of PLISTS override the values of previous elements.
  1587. This takes into account some special considerations for certain
  1588. parameters when merging lists."
  1589. (let ((results-exclusive-groups
  1590. '(("file" "list" "vector" "table" "scalar" "raw" "org"
  1591. "html" "latex" "code" "pp" "wrap")
  1592. ("replace" "silent" "append" "prepend")
  1593. ("output" "value")))
  1594. (exports-exclusive-groups
  1595. '(("code" "results" "both" "none")))
  1596. params results exports tangle noweb cache vars shebang comments padline)
  1597. (flet ((e-merge (exclusive-groups &rest result-params)
  1598. ;; maintain exclusivity of mutually exclusive parameters
  1599. (let (output)
  1600. (mapc (lambda (new-params)
  1601. (mapc (lambda (new-param)
  1602. (mapc (lambda (exclusive-group)
  1603. (when (member new-param exclusive-group)
  1604. (mapcar (lambda (excluded-param)
  1605. (setq output
  1606. (delete
  1607. excluded-param
  1608. output)))
  1609. exclusive-group)))
  1610. exclusive-groups)
  1611. (setq output (org-uniquify
  1612. (cons new-param output))))
  1613. new-params))
  1614. result-params)
  1615. output)))
  1616. (mapc
  1617. (lambda (plist)
  1618. (mapc
  1619. (lambda (pair)
  1620. (case (car pair)
  1621. (:var
  1622. (let ((name (if (listp (cdr pair))
  1623. (cadr pair)
  1624. (and (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*="
  1625. (cdr pair))
  1626. (intern (match-string 1 (cdr pair)))))))
  1627. (when name
  1628. (setq vars
  1629. (cons (cons name pair)
  1630. (if (member name (mapcar #'car vars))
  1631. (delq nil
  1632. (mapcar
  1633. (lambda (p) (unless (equal (car p) name) p))
  1634. vars))
  1635. vars))))))
  1636. (:results
  1637. (setq results (e-merge results-exclusive-groups
  1638. results
  1639. (split-string
  1640. (let ((r (cdr pair)))
  1641. (if (stringp r) r (eval r)))))))
  1642. (:file
  1643. (when (cdr pair)
  1644. (setq results (e-merge results-exclusive-groups
  1645. results '("file")))
  1646. (unless (or (member "both" exports)
  1647. (member "none" exports)
  1648. (member "code" exports))
  1649. (setq exports (e-merge exports-exclusive-groups
  1650. exports '("results"))))
  1651. (setq params (cons pair (assq-delete-all (car pair) params)))))
  1652. (:exports
  1653. (setq exports (e-merge exports-exclusive-groups
  1654. exports (split-string (cdr pair)))))
  1655. (:tangle ;; take the latest -- always overwrite
  1656. (setq tangle (or (list (cdr pair)) tangle)))
  1657. (:noweb
  1658. (setq noweb (e-merge '(("yes" "no" "tangle")) noweb
  1659. (split-string (or (cdr pair) "")))))
  1660. (:cache
  1661. (setq cache (e-merge '(("yes" "no")) cache
  1662. (split-string (or (cdr pair) "")))))
  1663. (:padline
  1664. (setq padline (e-merge '(("yes" "no")) padline
  1665. (split-string (or (cdr pair) "")))))
  1666. (:shebang ;; take the latest -- always overwrite
  1667. (setq shebang (or (list (cdr pair)) shebang)))
  1668. (:comments
  1669. (setq comments (e-merge '(("yes" "no")) comments
  1670. (split-string (or (cdr pair) "")))))
  1671. (t ;; replace: this covers e.g. :session
  1672. (setq params (cons pair (assq-delete-all (car pair) params))))))
  1673. plist))
  1674. plists))
  1675. (while vars (setq params (cons (cons :var (cddr (pop vars))) params)))
  1676. (mapc
  1677. (lambda (hd)
  1678. (let ((key (intern (concat ":" (symbol-name hd))))
  1679. (val (eval hd)))
  1680. (setf params (cons (cons key (mapconcat 'identity val " ")) params))))
  1681. '(results exports tangle noweb padline cache shebang comments))
  1682. params))
  1683. (defun org-babel-expand-noweb-references (&optional info parent-buffer)
  1684. "Expand Noweb references in the body of the current source code block.
  1685. For example the following reference would be replaced with the
  1686. body of the source-code block named 'example-block'.
  1687. <<example-block>>
  1688. Note that any text preceding the <<foo>> construct on a line will
  1689. be interposed between the lines of the replacement text. So for
  1690. example if <<foo>> is placed behind a comment, then the entire
  1691. replacement text will also be commented.
  1692. This function must be called from inside of the buffer containing
  1693. the source-code block which holds BODY.
  1694. In addition the following syntax can be used to insert the
  1695. results of evaluating the source-code block named 'example-block'.
  1696. <<example-block()>>
  1697. Any optional arguments can be passed to example-block by placing
  1698. the arguments inside the parenthesis following the convention
  1699. defined by `org-babel-lob'. For example
  1700. <<example-block(a=9)>>
  1701. would set the value of argument \"a\" equal to \"9\". Note that
  1702. these arguments are not evaluated in the current source-code
  1703. block but are passed literally to the \"example-block\"."
  1704. (let* ((parent-buffer (or parent-buffer (current-buffer)))
  1705. (info (or info (org-babel-get-src-block-info)))
  1706. (lang (nth 0 info))
  1707. (body (nth 1 info))
  1708. (comment (string= "noweb" (cdr (assoc :comments (nth 2 info)))))
  1709. (new-body "") index source-name evaluate prefix blocks-in-buffer)
  1710. (flet ((nb-add (text) (setq new-body (concat new-body text)))
  1711. (c-wrap (text)
  1712. (with-temp-buffer
  1713. (funcall (intern (concat lang "-mode")))
  1714. (comment-region (point) (progn (insert text) (point)))
  1715. (org-babel-trim (buffer-string))))
  1716. (blocks () ;; return the info lists of all blocks in this buffer
  1717. (let (infos)
  1718. (save-restriction
  1719. (widen)
  1720. (org-babel-map-src-blocks nil
  1721. (setq infos (cons (org-babel-get-src-block-info 'light)
  1722. infos))))
  1723. (reverse infos))))
  1724. (with-temp-buffer
  1725. (insert body) (goto-char (point-min))
  1726. (setq index (point))
  1727. (while (and (re-search-forward "<<\\(.+?\\)>>" nil t))
  1728. (save-match-data (setf source-name (match-string 1)))
  1729. (save-match-data (setq evaluate (string-match "\(.*\)" source-name)))
  1730. (save-match-data
  1731. (setq prefix
  1732. (buffer-substring (match-beginning 0)
  1733. (save-excursion
  1734. (beginning-of-line 1) (point)))))
  1735. ;; add interval to new-body (removing noweb reference)
  1736. (goto-char (match-beginning 0))
  1737. (nb-add (buffer-substring index (point)))
  1738. (goto-char (match-end 0))
  1739. (setq index (point))
  1740. (nb-add
  1741. (with-current-buffer parent-buffer
  1742. (mapconcat ;; interpose PREFIX between every line
  1743. #'identity
  1744. (split-string
  1745. (if evaluate
  1746. (let ((raw (org-babel-ref-resolve source-name)))
  1747. (if (stringp raw) raw (format "%S" raw)))
  1748. (or
  1749. ;; retrieve from the library of babel
  1750. (nth 2 (assoc (intern source-name)
  1751. org-babel-library-of-babel))
  1752. ;; find the expansion of reference in this buffer
  1753. (or (mapconcat
  1754. (lambda (i)
  1755. (when (string= source-name
  1756. (or (cdr (assoc :noweb-ref (nth 2 i)))
  1757. (nth 4 i)))
  1758. (let ((body (org-babel-expand-noweb-references i)))
  1759. (if comment
  1760. ((lambda (cs) (concat (c-wrap (car cs)) "\n"
  1761. body "\n" (c-wrap (cadr cs))))
  1762. (org-babel-tangle-comment-links i))
  1763. body))))
  1764. (or blocks-in-buffer
  1765. (setq blocks-in-buffer (blocks)))
  1766. "")
  1767. ;; possibly raise an error if named block doesn't exist
  1768. (if (member lang org-babel-noweb-error-langs)
  1769. (error "%s" (concat
  1770. "<<" source-name ">> "
  1771. "could not be resolved (see "
  1772. "`org-babel-noweb-error-langs')"))
  1773. ""))))
  1774. "[\n\r]") (concat "\n" prefix)))))
  1775. (nb-add (buffer-substring index (point-max)))))
  1776. new-body))
  1777. (defun org-babel-clean-text-properties (text)
  1778. "Strip all properties from text return."
  1779. (when text
  1780. (set-text-properties 0 (length text) nil text) text))
  1781. (defun org-babel-strip-protective-commas (body)
  1782. "Strip protective commas from bodies of source blocks."
  1783. (replace-regexp-in-string "^,#" "#" body))
  1784. (defun org-babel-script-escape (str)
  1785. "Safely convert tables into elisp lists."
  1786. (let (in-single in-double out)
  1787. ((lambda (escaped) (condition-case nil (org-babel-read escaped) (error escaped)))
  1788. (if (and (stringp str)
  1789. (> (length str) 2)
  1790. (string-equal "[" (substring str 0 1))
  1791. (string-equal "]" (substring str -1)))
  1792. (org-babel-read
  1793. (concat
  1794. "'"
  1795. (progn
  1796. (mapc
  1797. (lambda (ch)
  1798. (setq
  1799. out
  1800. (case ch
  1801. (91 (if (or in-double in-single) ; [
  1802. (cons 91 out)
  1803. (cons 40 out)))
  1804. (93 (if (or in-double in-single) ; ]
  1805. (cons 93 out)
  1806. (cons 41 out)))
  1807. (44 (if (or in-double in-single) ; ,
  1808. (cons 44 out) (cons 32 out)))
  1809. (39 (if in-double ; '
  1810. (cons 39 out)
  1811. (setq in-single (not in-single)) (cons 34 out)))
  1812. (34 (if in-single ; "
  1813. (append (list 34 32) out)
  1814. (setq in-double (not in-double)) (cons 34 out)))
  1815. (t (cons ch out)))))
  1816. (string-to-list str))
  1817. (apply #'string (reverse out)))))
  1818. str))))
  1819. (defun org-babel-read (cell &optional inhibit-lisp-eval)
  1820. "Convert the string value of CELL to a number if appropriate.
  1821. Otherwise if cell looks like lisp (meaning it starts with a
  1822. \"(\", \"'\", \"`\" or a \"[\") then read it as lisp, otherwise
  1823. return it unmodified as a string. Optional argument NO-LISP-EVAL
  1824. inhibits lisp evaluation for situations in which is it not
  1825. appropriate."
  1826. (if (and (stringp cell) (not (equal cell "")))
  1827. (or (org-babel-number-p cell)
  1828. (if (and (not inhibit-lisp-eval)
  1829. (member (substring cell 0 1) '("(" "'" "`" "[")))
  1830. (eval (read cell))
  1831. (if (string= (substring cell 0 1) "\"")
  1832. (read cell)
  1833. (progn (set-text-properties 0 (length cell) nil cell) cell))))
  1834. cell))
  1835. (defun org-babel-number-p (string)
  1836. "If STRING represents a number return it's value."
  1837. (if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string)
  1838. (= (length (substring string (match-beginning 0)
  1839. (match-end 0)))
  1840. (length string)))
  1841. (string-to-number string)))
  1842. (defun org-babel-import-elisp-from-file (file-name &optional separator)
  1843. "Read the results located at FILE-NAME into an elisp table.
  1844. If the table is trivial, then return it as a scalar."
  1845. (let (result)
  1846. (save-window-excursion
  1847. (with-temp-buffer
  1848. (condition-case nil
  1849. (progn
  1850. (org-table-import file-name separator)
  1851. (delete-file file-name)
  1852. (setq result (mapcar (lambda (row)
  1853. (mapcar #'org-babel-string-read row))
  1854. (org-table-to-lisp))))
  1855. (error nil)))
  1856. (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
  1857. (if (consp (car result))
  1858. (if (null (cdr (car result)))
  1859. (caar result)
  1860. result)
  1861. (car result))
  1862. result))))
  1863. (defun org-babel-string-read (cell)
  1864. "Strip nested \"s from around strings."
  1865. (org-babel-read (or (and (stringp cell)
  1866. (string-match "\\\"\\(.+\\)\\\"" cell)
  1867. (match-string 1 cell))
  1868. cell)))
  1869. (defun org-babel-reverse-string (string)
  1870. "Return the reverse of STRING."
  1871. (apply 'string (reverse (string-to-list string))))
  1872. (defun org-babel-chomp (string &optional regexp)
  1873. "Strip trailing spaces and carriage returns from STRING.
  1874. Default regexp used is \"[ \f\t\n\r\v]\" but can be
  1875. overwritten by specifying a regexp as a second argument."
  1876. (let ((regexp (or regexp "[ \f\t\n\r\v]")))
  1877. (while (and (> (length string) 0)
  1878. (string-match regexp (substring string -1)))
  1879. (setq string (substring string 0 -1)))
  1880. string))
  1881. (defun org-babel-trim (string &optional regexp)
  1882. "Strip leading and trailing spaces and carriage returns from STRING.
  1883. Like `org-babel-chomp' only it runs on both the front and back
  1884. of the string."
  1885. (org-babel-chomp (org-babel-reverse-string
  1886. (org-babel-chomp (org-babel-reverse-string string) regexp))
  1887. regexp))
  1888. (defvar org-babel-org-babel-call-process-region-original nil)
  1889. (defun org-babel-tramp-handle-call-process-region
  1890. (start end program &optional delete buffer display &rest args)
  1891. "Use tramp to handle call-process-region.
  1892. Fixes a bug in `tramp-handle-call-process-region'."
  1893. (if (and (featurep 'tramp) (file-remote-p default-directory))
  1894. (let ((tmpfile (tramp-compat-make-temp-file "")))
  1895. (write-region start end tmpfile)
  1896. (when delete (delete-region start end))
  1897. (unwind-protect
  1898. ;; (apply 'call-process program tmpfile buffer display args)
  1899. ;; bug in tramp
  1900. (apply 'process-file program tmpfile buffer display args)
  1901. (delete-file tmpfile)))
  1902. ;; org-babel-call-process-region-original is the original emacs
  1903. ;; definition. It is in scope from the let binding in
  1904. ;; org-babel-execute-src-block
  1905. (apply org-babel-call-process-region-original
  1906. start end program delete buffer display args)))
  1907. (defun org-babel-local-file-name (file)
  1908. "Return the local name component of FILE."
  1909. (if (file-remote-p file)
  1910. (let (localname)
  1911. (with-parsed-tramp-file-name file nil
  1912. localname))
  1913. file))
  1914. (defun org-babel-process-file-name (name &optional no-quote-p)
  1915. "Prepare NAME to be used in an external process.
  1916. If NAME specifies a remote location, the remote portion of the
  1917. name is removed, since in that case the process will be executing
  1918. remotely. The file name is then processed by
  1919. `expand-file-name'. Unless second argument NO-QUOTE-P is non-nil,
  1920. the file name is additionally processed by
  1921. `shell-quote-argument'"
  1922. ((lambda (f) (if no-quote-p f (shell-quote-argument f)))
  1923. (expand-file-name (org-babel-local-file-name name))))
  1924. (defvar org-babel-temporary-directory)
  1925. (unless (or noninteractive (boundp 'org-babel-temporary-directory))
  1926. (defvar org-babel-temporary-directory
  1927. (or (and (boundp 'org-babel-temporary-directory)
  1928. (file-exists-p org-babel-temporary-directory)
  1929. org-babel-temporary-directory)
  1930. (make-temp-file "babel-" t))
  1931. "Directory to hold temporary files created to execute code blocks.
  1932. Used by `org-babel-temp-file'. This directory will be removed on
  1933. Emacs shutdown."))
  1934. (defun org-babel-temp-file (prefix &optional suffix)
  1935. "Create a temporary file in the `org-babel-temporary-directory'.
  1936. Passes PREFIX and SUFFIX directly to `make-temp-file' with the
  1937. value of `temporary-file-directory' temporarily set to the value
  1938. of `org-babel-temporary-directory'."
  1939. (if (file-remote-p default-directory)
  1940. (make-temp-file
  1941. (concat (file-remote-p default-directory)
  1942. (expand-file-name
  1943. prefix temporary-file-directory)
  1944. nil suffix))
  1945. (let ((temporary-file-directory
  1946. (or (and (boundp 'org-babel-temporary-directory)
  1947. (file-exists-p org-babel-temporary-directory)
  1948. org-babel-temporary-directory)
  1949. temporary-file-directory)))
  1950. (make-temp-file prefix nil suffix))))
  1951. (defun org-babel-remove-temporary-directory ()
  1952. "Remove `org-babel-temporary-directory' on Emacs shutdown."
  1953. (when (and (boundp 'org-babel-temporary-directory)
  1954. (file-exists-p org-babel-temporary-directory))
  1955. ;; taken from `delete-directory' in files.el
  1956. (condition-case nil
  1957. (progn
  1958. (mapc (lambda (file)
  1959. ;; This test is equivalent to
  1960. ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
  1961. ;; but more efficient
  1962. (if (eq t (car (file-attributes file)))
  1963. (delete-directory file)
  1964. (delete-file file)))
  1965. ;; We do not want to delete "." and "..".
  1966. (directory-files org-babel-temporary-directory 'full
  1967. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
  1968. (delete-directory org-babel-temporary-directory))
  1969. (error
  1970. (message "Failed to remove temporary Org-babel directory %s"
  1971. (if (boundp 'org-babel-temporary-directory)
  1972. org-babel-temporary-directory
  1973. "[directory not defined]"))))))
  1974. (add-hook 'kill-emacs-hook 'org-babel-remove-temporary-directory)
  1975. (provide 'ob)
  1976. ;; arch-tag: 01a7ebee-06c5-4ee4-a709-e660d28c0af1
  1977. ;;; ob.el ends here