ob.el 81 KB

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