ob-core.el 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145
  1. ;;; ob-core.el --- working with code blocks in org-mode
  2. ;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. ;; Authors: Eric Schulte
  4. ;; Dan Davison
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: http://orgmode.org
  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. ;;; Code:
  19. (eval-when-compile
  20. (require 'cl))
  21. (require 'cl-lib)
  22. (require 'ob-eval)
  23. (require 'org-macs)
  24. (require 'org-compat)
  25. (defconst org-babel-exeext
  26. (if (memq system-type '(windows-nt cygwin))
  27. ".exe"
  28. nil))
  29. ;; dynamically scoped for tramp
  30. (defvar org-babel-call-process-region-original nil)
  31. (defvar org-src-lang-modes)
  32. (defvar org-babel-library-of-babel)
  33. (declare-function outline-show-all "outline" ())
  34. (declare-function org-remove-indentation "org" (code &optional n))
  35. (declare-function org-mark-ring-push "org" (&optional pos buffer))
  36. (declare-function tramp-compat-make-temp-file "tramp-compat"
  37. (filename &optional dir-flag))
  38. (declare-function tramp-dissect-file-name "tramp" (name &optional nodefault))
  39. (declare-function tramp-file-name-user "tramp" (vec))
  40. (declare-function tramp-file-name-host "tramp" (vec))
  41. (declare-function with-parsed-tramp-file-name "tramp" (filename var &rest body))
  42. (declare-function org-icompleting-read "org" (&rest args))
  43. (declare-function org-edit-src-code "org-src" (&optional code edit-buffer-name))
  44. (declare-function org-edit-src-exit "org-src" ())
  45. (declare-function org-open-at-point "org" (&optional in-emacs reference-buffer))
  46. (declare-function org-save-outline-visibility "org-macs" (use-markers &rest body))
  47. (declare-function org-outline-overlay-data "org" (&optional use-markers))
  48. (declare-function org-set-outline-overlay-data "org" (data))
  49. (declare-function org-narrow-to-subtree "org" ())
  50. (declare-function org-split-string "org" (string &optional separators))
  51. (declare-function org-entry-get "org"
  52. (pom property &optional inherit literal-nil))
  53. (declare-function org-make-options-regexp "org" (kwds &optional extra))
  54. (declare-function org-do-remove-indentation "org" (&optional n))
  55. (declare-function org-next-block "org" (arg &optional backward block-regexp))
  56. (declare-function org-previous-block "org" (arg &optional block-regexp))
  57. (declare-function org-show-context "org" (&optional key))
  58. (declare-function org-at-table-p "org" (&optional table-type))
  59. (declare-function org-cycle "org" (&optional arg))
  60. (declare-function org-uniquify "org" (list))
  61. (declare-function org-current-level "org" ())
  62. (declare-function org-table-import "org-table" (file arg))
  63. (declare-function org-add-hook "org-compat"
  64. (hook function &optional append local))
  65. (declare-function org-table-align "org-table" ())
  66. (declare-function org-table-end "org-table" (&optional table-type))
  67. (declare-function orgtbl-to-generic "org-table" (table params))
  68. (declare-function orgtbl-to-orgtbl "org-table" (table params))
  69. (declare-function org-babel-tangle-comment-links "ob-tangle" (&optional info))
  70. (declare-function org-babel-lob-get-info "ob-lob" nil)
  71. (declare-function org-babel-ref-split-args "ob-ref" (arg-string))
  72. (declare-function org-babel-ref-parse "ob-ref" (assignment))
  73. (declare-function org-babel-ref-resolve "ob-ref" (ref))
  74. (declare-function org-babel-ref-goto-headline-id "ob-ref" (id))
  75. (declare-function org-babel-ref-headline-body "ob-ref" ())
  76. (declare-function org-babel-lob-execute-maybe "ob-lob" ())
  77. (declare-function org-number-sequence "org-compat" (from &optional to inc))
  78. (declare-function org-at-item-p "org-list" ())
  79. (declare-function org-list-to-generic "org-list" (LIST PARAMS))
  80. (declare-function org-list-to-lisp "org-list" (&optional delete))
  81. (declare-function org-list-struct "org-list" ())
  82. (declare-function org-list-prevs-alist "org-list" (struct))
  83. (declare-function org-list-get-list-end "org-list" (item struct prevs))
  84. (declare-function org-completing-read "org" (&rest args))
  85. (declare-function org-escape-code-in-region "org-src" (beg end))
  86. (declare-function org-unescape-code-in-string "org-src" (s))
  87. (declare-function org-table-to-lisp "org-table" (&optional txt))
  88. (declare-function org-reverse-string "org" (string))
  89. (declare-function org-element-context "org-element" (&optional element))
  90. (declare-function org-element-type "org-element" (element))
  91. (declare-function org-element-at-point "org-element" ())
  92. (declare-function org-element-property "org-element" (property element))
  93. (declare-function org-macro-escape-arguments "org-macro" (&rest args))
  94. (defgroup org-babel nil
  95. "Code block evaluation and management in `org-mode' documents."
  96. :tag "Babel"
  97. :group 'org)
  98. (defcustom org-confirm-babel-evaluate t
  99. "Confirm before evaluation.
  100. \\<org-mode-map>\
  101. Require confirmation before interactively evaluating code
  102. blocks in Org-mode buffers. The default value of this variable
  103. is t, meaning confirmation is required for any code block
  104. evaluation. This variable can be set to nil to inhibit any
  105. future confirmation requests. This variable can also be set to a
  106. function which takes two arguments the language of the code block
  107. and the body of the code block. Such a function should then
  108. return a non-nil value if the user should be prompted for
  109. execution or nil if no prompt is required.
  110. Warning: Disabling confirmation may result in accidental
  111. evaluation of potentially harmful code. It may be advisable
  112. remove code block execution from \\[org-ctrl-c-ctrl-c] \
  113. as further protection
  114. against accidental code block evaluation. The
  115. `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can be used to
  116. remove code block execution from the \\[org-ctrl-c-ctrl-c] keybinding."
  117. :group 'org-babel
  118. :version "24.1"
  119. :type '(choice boolean function))
  120. ;; don't allow this variable to be changed through file settings
  121. (put 'org-confirm-babel-evaluate 'safe-local-variable (lambda (x) (eq x t)))
  122. (defcustom org-babel-no-eval-on-ctrl-c-ctrl-c nil
  123. "\\<org-mode-map>\
  124. Remove code block evaluation from the \\[org-ctrl-c-ctrl-c] key binding."
  125. :group 'org-babel
  126. :version "24.1"
  127. :type 'boolean)
  128. (defcustom org-babel-results-keyword "RESULTS"
  129. "Keyword used to name results generated by code blocks.
  130. It should be \"RESULTS\". However any capitalization may be
  131. used."
  132. :group 'org-babel
  133. :version "24.4"
  134. :package-version '(Org . "8.0")
  135. :type 'string
  136. :safe (lambda (v)
  137. (and (stringp v)
  138. (eq (compare-strings "RESULTS" nil nil v nil nil t)
  139. t))))
  140. (defcustom org-babel-noweb-wrap-start "<<"
  141. "String used to begin a noweb reference in a code block.
  142. See also `org-babel-noweb-wrap-end'."
  143. :group 'org-babel
  144. :type 'string)
  145. (defcustom org-babel-noweb-wrap-end ">>"
  146. "String used to end a noweb reference in a code block.
  147. See also `org-babel-noweb-wrap-start'."
  148. :group 'org-babel
  149. :type 'string)
  150. (defcustom org-babel-inline-result-wrap "=%s="
  151. "Format string used to wrap inline results.
  152. This string must include a \"%s\" which will be replaced by the results."
  153. :group 'org-babel
  154. :type 'string)
  155. (put 'org-babel-inline-result-wrap
  156. 'safe-local-variable
  157. (lambda (value)
  158. (and (stringp value)
  159. (string-match-p "%s" value))))
  160. (defun org-babel-noweb-wrap (&optional regexp)
  161. (concat org-babel-noweb-wrap-start
  162. (or regexp "\\([^ \t\n].+?[^ \t]\\|[^ \t\n]\\)")
  163. org-babel-noweb-wrap-end))
  164. (defvar org-babel-src-name-regexp
  165. "^[ \t]*#\\+name:[ \t]*"
  166. "Regular expression used to match a source name line.")
  167. (defvar org-babel-multi-line-header-regexp
  168. "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
  169. "Regular expression used to match multi-line header arguments.")
  170. (defvar org-babel-src-block-regexp
  171. (concat
  172. ;; (1) indentation (2) lang
  173. "^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
  174. ;; (3) switches
  175. "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
  176. ;; (4) header arguments
  177. "\\([^\n]*\\)\n"
  178. ;; (5) body
  179. "\\([^\000]*?\n\\)??[ \t]*#\\+end_src")
  180. "Regexp used to identify code blocks.")
  181. (defvar org-babel-inline-src-block-regexp
  182. (concat
  183. ;; (1) replacement target (2) lang
  184. "\\(?:^\\|[^-[:alnum:]]?\\)\\(src_\\([^ \f\t\n\r\v[]+\\)"
  185. ;; (3,4) (unused, headers)
  186. "\\(\\|\\[[ \t]*\\(.*?\\)\\]\\)"
  187. ;; (5) body
  188. "{\\([^\f\n\r\v]+?\\)}\\)")
  189. "Regexp used to identify inline src-blocks.")
  190. (defun org-babel--get-vars (params)
  191. "Return the babel variable assignments in PARAMS.
  192. PARAMS is a quasi-alist of header args, whcih may contain
  193. multiple entries for the key `:var'. This function returns a
  194. list of the cdr of all the `:var' entries."
  195. (mapcar #'cdr
  196. (cl-remove-if-not (lambda (x) (eq (car x) :var)) params)))
  197. (defun org-babel-get-inline-src-block-matches ()
  198. "Set match data if within body of an inline source block.
  199. Returns non-nil if match-data set"
  200. (save-excursion
  201. (let ((datum (org-element-context)))
  202. (when (eq (org-element-type datum) 'inline-src-block)
  203. (goto-char (org-element-property :begin datum))
  204. (when (looking-at org-babel-inline-src-block-regexp)
  205. t )))))
  206. (defvar org-babel-inline-lob-one-liner-regexp)
  207. (defun org-babel-get-lob-one-liner-matches ()
  208. "Set match data if on line of an lob one liner.
  209. Returns non-nil if match-data set"
  210. (save-excursion
  211. (let ((datum (org-element-context)))
  212. (when (eq (org-element-type datum) 'inline-babel-call)
  213. (goto-char (org-element-property :begin datum))))
  214. (if (looking-at org-babel-inline-lob-one-liner-regexp)
  215. t
  216. nil)))
  217. (defun org-babel-get-src-block-info (&optional light)
  218. "Get information on the current source block.
  219. Optional argument LIGHT does not resolve remote variable
  220. references; a process which could likely result in the execution
  221. of other code blocks.
  222. Returns a list
  223. (language body header-arguments-alist switches name indent block-head)."
  224. (let ((case-fold-search t) head info name indent)
  225. ;; full code block
  226. (if (setq head (org-babel-where-is-src-block-head))
  227. (save-excursion
  228. (goto-char head)
  229. (setq info (org-babel-parse-src-block-match))
  230. (setq indent (car (last info)))
  231. (setq info (butlast info))
  232. (while (and (forward-line -1)
  233. (looking-at org-babel-multi-line-header-regexp))
  234. (setf (nth 2 info)
  235. (org-babel-merge-params
  236. (nth 2 info)
  237. (org-babel-parse-header-arguments (match-string 1)))))
  238. (when (looking-at (org-babel-named-src-block-regexp-for-name))
  239. (setq name (org-match-string-no-properties 9))))
  240. ;; inline source block
  241. (when (org-babel-get-inline-src-block-matches)
  242. (setq head (match-beginning 0))
  243. (setq info (org-babel-parse-inline-src-block-match))))
  244. ;; resolve variable references and add summary parameters
  245. (when (and info (not light))
  246. (setf (nth 2 info) (org-babel-process-params (nth 2 info))))
  247. (when info
  248. (setf (nth 2 info) (org-babel-generate-file-param name (nth 2 info))))
  249. (when info (append info (list name indent head)))))
  250. (defvar org-babel-exp-reference-buffer nil
  251. "Buffer containing original contents of the exported buffer.
  252. This is used by Babel to resolve references in source blocks.
  253. Its value is dynamically bound during export.")
  254. (defun org-babel-check-confirm-evaluate (info)
  255. "Check whether INFO allows code block evaluation.
  256. Returns nil if evaluation is disallowed, t if it is
  257. unconditionally allowed, and the symbol `query' if the user
  258. should be asked whether to allow evaluation."
  259. (let* ((headers (nth 2 info))
  260. (eval (or (cdr (assq :eval headers))
  261. (when (assq :noeval headers) "no")))
  262. (eval-no (member eval '("no" "never")))
  263. (export org-babel-exp-reference-buffer)
  264. (eval-no-export (and export (member eval '("no-export" "never-export"))))
  265. (noeval (or eval-no eval-no-export))
  266. (query (or (equal eval "query")
  267. (and export (equal eval "query-export"))
  268. (if (functionp org-confirm-babel-evaluate)
  269. (save-excursion
  270. (goto-char (nth 6 info))
  271. (funcall org-confirm-babel-evaluate
  272. ;; language, code block body
  273. (nth 0 info) (nth 1 info)))
  274. org-confirm-babel-evaluate))))
  275. (cond
  276. (noeval nil)
  277. (query 'query)
  278. (t t))))
  279. (defun org-babel-check-evaluate (info)
  280. "Check if code block INFO should be evaluated.
  281. Do not query the user, but do display an informative message if
  282. evaluation is blocked. Returns non-nil if evaluation is not blocked."
  283. (let ((evalp (org-babel-check-confirm-evaluate info)))
  284. (when (null evalp)
  285. (message "Evaluation of this %s code-block%sis disabled."
  286. (nth 0 info)
  287. (let ((name (nth 4 info))) (if name (format " (%s) " name) ""))))
  288. evalp))
  289. ;; Dynamically scoped for asynchronous export.
  290. (defvar org-babel-confirm-evaluate-answer-no)
  291. (defun org-babel-confirm-evaluate (info)
  292. "Confirm evaluation of the code block INFO.
  293. This query can also be suppressed by setting the value of
  294. `org-confirm-babel-evaluate' to nil, in which case all future
  295. interactive code block evaluations will proceed without any
  296. confirmation from the user.
  297. Note disabling confirmation may result in accidental evaluation
  298. of potentially harmful code.
  299. The variable `org-babel-confirm-evaluate-answer-no' is used by
  300. the async export process, which requires a non-interactive
  301. environment, to override this check."
  302. (let* ((evalp (org-babel-check-confirm-evaluate info))
  303. (lang (nth 0 info))
  304. (name (nth 4 info))
  305. (name-string (if name (format " (%s) " name) " ")))
  306. (pcase evalp
  307. (`nil nil)
  308. (`t t)
  309. (`query (or
  310. (and (not (org-bound-and-true-p
  311. org-babel-confirm-evaluate-answer-no))
  312. (yes-or-no-p
  313. (format "Evaluate this %s code block%son your system? "
  314. lang name-string)))
  315. (progn
  316. (message "Evaluation of this %s code-block%sis aborted."
  317. lang name-string)
  318. nil)))
  319. (x (error "Unexpected value `%s' from `org-babel-check-confirm-evaluate'" x)))))
  320. ;;;###autoload
  321. (defun org-babel-execute-safely-maybe ()
  322. (unless org-babel-no-eval-on-ctrl-c-ctrl-c
  323. (org-babel-execute-maybe)))
  324. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-safely-maybe)
  325. ;;;###autoload
  326. (defun org-babel-execute-maybe ()
  327. (interactive)
  328. (or (org-babel-execute-src-block-maybe)
  329. (org-babel-lob-execute-maybe)))
  330. (defmacro org-babel-when-in-src-block (&rest body)
  331. "Execute BODY if point is in a source block and return t.
  332. Otherwise do nothing and return nil."
  333. `(if (or (org-babel-where-is-src-block-head)
  334. (org-babel-get-inline-src-block-matches))
  335. (progn
  336. ,@body
  337. t)
  338. nil))
  339. (defun org-babel-execute-src-block-maybe ()
  340. "Conditionally execute a source block.
  341. Detect if this is context for a Babel src-block and if so
  342. then run `org-babel-execute-src-block'."
  343. (interactive)
  344. (org-babel-when-in-src-block
  345. (org-babel-eval-wipe-error-buffer)
  346. (org-babel-execute-src-block current-prefix-arg)))
  347. ;;;###autoload
  348. (defun org-babel-view-src-block-info ()
  349. "Display information on the current source block.
  350. This includes header arguments, language and name, and is largely
  351. a window into the `org-babel-get-src-block-info' function."
  352. (interactive)
  353. (let ((info (org-babel-get-src-block-info 'light))
  354. (full (lambda (it) (> (length it) 0)))
  355. (printf (lambda (fmt &rest args) (princ (apply #'format fmt args)))))
  356. (when info
  357. (with-help-window (help-buffer)
  358. (let ((name (nth 4 info))
  359. (lang (nth 0 info))
  360. (switches (nth 3 info))
  361. (header-args (nth 2 info)))
  362. (when name (funcall printf "Name: %s\n" name))
  363. (when lang (funcall printf "Lang: %s\n" lang))
  364. (funcall printf "Properties:\n")
  365. (funcall printf "\t:header-args \t%s\n" (org-entry-get (point) "header-args" t))
  366. (funcall printf "\t:header-args:%s \t%s\n" lang (org-entry-get (point) (concat "header-args:" lang) t))
  367. (when (funcall full switches) (funcall printf "Switches: %s\n" switches))
  368. (funcall printf "Header Arguments:\n")
  369. (dolist (pair (sort header-args
  370. (lambda (a b) (string< (symbol-name (car a))
  371. (symbol-name (car b))))))
  372. (when (funcall full (format "%s" (cdr pair)))
  373. (funcall printf "\t%S%s\t%s\n"
  374. (car pair)
  375. (if (> (length (format "%S" (car pair))) 7) "" "\t")
  376. (cdr pair)))))))))
  377. ;;;###autoload
  378. (defun org-babel-expand-src-block-maybe ()
  379. "Conditionally expand a source block.
  380. Detect if this is context for a org-babel src-block and if so
  381. then run `org-babel-expand-src-block'."
  382. (interactive)
  383. (org-babel-when-in-src-block
  384. (org-babel-expand-src-block current-prefix-arg)))
  385. ;;;###autoload
  386. (defun org-babel-load-in-session-maybe ()
  387. "Conditionally load a source block in a session.
  388. Detect if this is context for a org-babel src-block and if so
  389. then run `org-babel-load-in-session'."
  390. (interactive)
  391. (org-babel-when-in-src-block
  392. (org-babel-load-in-session current-prefix-arg)))
  393. (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
  394. ;;;###autoload
  395. (defun org-babel-pop-to-session-maybe ()
  396. "Conditionally pop to a session.
  397. Detect if this is context for a org-babel src-block and if so
  398. then run `org-babel-switch-to-session'."
  399. (interactive)
  400. (org-babel-when-in-src-block
  401. (org-babel-switch-to-session current-prefix-arg)))
  402. (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
  403. (defconst org-babel-common-header-args-w-values
  404. '((cache . ((no yes)))
  405. (cmdline . :any)
  406. (colnames . ((nil no yes)))
  407. (comments . ((no link yes org both noweb)))
  408. (dir . :any)
  409. (eval . ((yes no no-export strip-export never-export eval never
  410. query)))
  411. (exports . ((code results both none)))
  412. (epilogue . :any)
  413. (file . :any)
  414. (file-desc . :any)
  415. (file-ext . :any)
  416. (hlines . ((no yes)))
  417. (mkdirp . ((yes no)))
  418. (no-expand)
  419. (noeval)
  420. (noweb . ((yes no tangle no-export strip-export)))
  421. (noweb-ref . :any)
  422. (noweb-sep . :any)
  423. (output-dir . :any)
  424. (padline . ((yes no)))
  425. (post . :any)
  426. (prologue . :any)
  427. (results . ((file list vector table scalar verbatim)
  428. (raw html latex org code pp drawer)
  429. (replace silent none append prepend)
  430. (output value)))
  431. (rownames . ((no yes)))
  432. (sep . :any)
  433. (session . :any)
  434. (shebang . :any)
  435. (tangle . ((tangle yes no :any)))
  436. (tangle-mode . ((#o755 #o555 #o444 :any)))
  437. (var . :any)
  438. (wrap . :any)))
  439. (defconst org-babel-header-arg-names
  440. (mapcar #'car org-babel-common-header-args-w-values)
  441. "Common header arguments used by org-babel.
  442. Note that individual languages may define their own language
  443. specific header arguments as well.")
  444. (defconst org-babel-safe-header-args
  445. '(:cache :colnames :comments :exports :epilogue :hlines :noeval
  446. :noweb :noweb-ref :noweb-sep :padline :prologue :rownames
  447. :sep :session :tangle :wrap
  448. (:eval . ("never" "query"))
  449. (:results . (lambda (str) (not (string-match "file" str)))))
  450. "A list of safe header arguments for babel source blocks.
  451. The list can have entries of the following forms:
  452. - :ARG -> :ARG is always a safe header arg
  453. - (:ARG . (VAL1 VAL2 ...)) -> :ARG is safe as a header arg if it is
  454. `equal' to one of the VALs.
  455. - (:ARG . FN) -> :ARG is safe as a header arg if the function FN
  456. returns non-nil. FN is passed one
  457. argument, the value of the header arg
  458. (as a string).")
  459. (defmacro org-babel-header-args-safe-fn (safe-list)
  460. "Return a function that determines whether a list of header args are safe.
  461. Intended usage is:
  462. \(put \\='org-babel-default-header-args \\='safe-local-variable
  463. (org-babel-header-args-safe-p org-babel-safe-header-args)
  464. This allows org-babel languages to extend the list of safe values for
  465. their `org-babel-default-header-args:foo' variable.
  466. For the format of SAFE-LIST, see `org-babel-safe-header-args'."
  467. `(lambda (value)
  468. (and (listp value)
  469. (cl-every
  470. (lambda (pair)
  471. (and (consp pair)
  472. (org-babel-one-header-arg-safe-p pair ,safe-list)))
  473. value))))
  474. (defvar org-babel-default-header-args
  475. '((:session . "none") (:results . "replace") (:exports . "code")
  476. (:cache . "no") (:noweb . "no") (:hlines . "no") (:tangle . "no"))
  477. "Default arguments to use when evaluating a source block.")
  478. (put 'org-babel-default-header-args 'safe-local-variable
  479. (org-babel-header-args-safe-fn org-babel-safe-header-args))
  480. (defvar org-babel-default-inline-header-args
  481. '((:session . "none") (:results . "replace")
  482. (:exports . "results") (:hlines . "yes"))
  483. "Default arguments to use when evaluating an inline source block.")
  484. (put 'org-babel-default-inline-header-args 'safe-local-variable
  485. (org-babel-header-args-safe-fn org-babel-safe-header-args))
  486. (defvar org-babel-data-names '("tblname" "results" "name"))
  487. (defvar org-babel-result-regexp
  488. (concat "^[ \t]*#\\+"
  489. (regexp-opt org-babel-data-names t)
  490. "\\(\\[\\("
  491. ;; FIXME The string below is `org-ts-regexp'
  492. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  493. " \\)?\\([[:alnum:]]+\\)\\]\\)?\\:[ \t]*")
  494. "Regular expression used to match result lines.
  495. If the results are associated with a hash key then the hash will
  496. be saved in the second match data.")
  497. (defvar org-babel-result-w-name-regexp
  498. (concat org-babel-result-regexp
  499. "\\([^ ()\f\t\n\r\v]+\\)\\((\\(.*\\))\\|\\)"))
  500. (defvar org-babel-min-lines-for-block-output 10
  501. "The minimum number of lines for block output.
  502. If number of lines of output is equal to or exceeds this
  503. value, the output is placed in a #+begin_example...#+end_example
  504. block. Otherwise the output is marked as literal by inserting
  505. colons at the starts of the lines. This variable only takes
  506. effect if the :results output option is in effect.")
  507. (defvar org-babel-noweb-error-all-langs nil
  508. "Raise errors when noweb references don't resolve.
  509. Also see `org-babel-noweb-error-langs' to control noweb errors on
  510. a language by language bases.")
  511. (defvar org-babel-noweb-error-langs nil
  512. "Languages for which Babel will raise literate programming errors.
  513. List of languages for which errors should be raised when the
  514. source code block satisfying a noweb reference in this language
  515. can not be resolved. Also see `org-babel-noweb-error-all-langs'
  516. to raise errors for all languages.")
  517. (defvar org-babel-hash-show 4
  518. "Number of initial characters to show of a hidden results hash.")
  519. (defvar org-babel-hash-show-time nil
  520. "Non-nil means show the time the code block was evaluated in the result hash.")
  521. (defvar org-babel-after-execute-hook nil
  522. "Hook for functions to be called after `org-babel-execute-src-block'")
  523. (defun org-babel-named-src-block-regexp-for-name (&optional name)
  524. "This generates a regexp used to match a src block named NAME.
  525. If NAME is nil, match any name. Matched name is then put in
  526. match group 9. Other match groups are defined in
  527. `org-babel-src-block-regexp'."
  528. (concat org-babel-src-name-regexp
  529. (concat (if name (regexp-quote name) "\\(?9:.*?\\)") "[ \t]*" )
  530. "\\(?:\n[ \t]*#\\+\\S-+:.*\\)*?"
  531. "\n"
  532. (substring org-babel-src-block-regexp 1)))
  533. (defun org-babel-named-data-regexp-for-name (name)
  534. "This generates a regexp used to match data named NAME."
  535. (concat org-babel-result-regexp (regexp-quote name) "\\([ \t]\\|$\\)"))
  536. ;;; functions
  537. (defvar call-process-region)
  538. (defvar org-babel-current-src-block-location nil
  539. "Marker pointing to the src block currently being executed.
  540. This may also point to a call line or an inline code block. If
  541. multiple blocks are being executed (e.g., in chained execution
  542. through use of the :var header argument) this marker points to
  543. the outer-most code block.")
  544. ;;;###autoload
  545. (defun org-babel-execute-src-block (&optional arg info params)
  546. "Execute the current source code block.
  547. Insert the results of execution into the buffer. Source code
  548. execution and the collection and formatting of results can be
  549. controlled through a variety of header arguments.
  550. With prefix argument ARG, force re-execution even if an existing
  551. result cached in the buffer would otherwise have been returned.
  552. Optionally supply a value for INFO in the form returned by
  553. `org-babel-get-src-block-info'.
  554. Optionally supply a value for PARAMS which will be merged with
  555. the header arguments specified at the front of the source code
  556. block."
  557. (interactive)
  558. (let* ((org-babel-current-src-block-location
  559. (or org-babel-current-src-block-location
  560. (nth 6 info)
  561. (org-babel-where-is-src-block-head)
  562. ;; inline src block
  563. (and (org-babel-get-inline-src-block-matches)
  564. (match-beginning 0))))
  565. (info (if info
  566. (copy-tree info)
  567. (org-babel-get-src-block-info))))
  568. (cl-callf org-babel-merge-params (nth 2 info) params)
  569. (when (org-babel-check-evaluate info)
  570. (cl-callf org-babel-process-params (nth 2 info))
  571. (let* ((params (nth 2 info))
  572. (cachep (and (not arg) (cdr (assoc :cache params))
  573. (string= "yes" (cdr (assoc :cache params)))))
  574. (new-hash (when cachep (org-babel-sha1-hash info)))
  575. (old-hash (when cachep (org-babel-current-result-hash)))
  576. (cache-current-p (and (not arg) new-hash
  577. (equal new-hash old-hash))))
  578. (cond
  579. (cache-current-p
  580. (save-excursion ;; return cached result
  581. (goto-char (org-babel-where-is-src-block-result nil info))
  582. (forward-line)
  583. (skip-chars-forward " \t")
  584. (let ((result (org-babel-read-result)))
  585. (message (replace-regexp-in-string
  586. "%" "%%" (format "%S" result))) result)))
  587. ((org-babel-confirm-evaluate info)
  588. (let* ((lang (nth 0 info))
  589. (result-params (cdr (assoc :result-params params)))
  590. (body (setf (nth 1 info)
  591. (if (org-babel-noweb-p params :eval)
  592. (org-babel-expand-noweb-references info)
  593. (nth 1 info))))
  594. (dir (cdr (assoc :dir params)))
  595. (default-directory
  596. (or (and dir (file-name-as-directory (expand-file-name dir)))
  597. default-directory))
  598. (org-babel-call-process-region-original ;; for tramp handler
  599. (or (org-bound-and-true-p
  600. org-babel-call-process-region-original)
  601. (symbol-function 'call-process-region)))
  602. (indent (nth 5 info))
  603. result cmd)
  604. (unwind-protect
  605. (let ((call-process-region
  606. (lambda (&rest args)
  607. (apply 'org-babel-tramp-handle-call-process-region
  608. args))))
  609. (let ((lang-check
  610. (lambda (f)
  611. (let ((f (intern (concat "org-babel-execute:" f))))
  612. (when (fboundp f) f)))))
  613. (setq cmd
  614. (or (funcall lang-check lang)
  615. (funcall lang-check
  616. (symbol-name
  617. (cdr (assoc lang org-src-lang-modes))))
  618. (error "No org-babel-execute function for %s!"
  619. lang))))
  620. (message "executing %s code block%s..."
  621. (capitalize lang)
  622. (if (nth 4 info) (format " (%s)" (nth 4 info)) ""))
  623. (if (member "none" result-params)
  624. (progn
  625. (funcall cmd body params)
  626. (message "result silenced")
  627. (setq result nil))
  628. (setq result
  629. (let ((result (funcall cmd body params)))
  630. (if (and (eq (cdr (assoc :result-type params))
  631. 'value)
  632. (or (member "vector" result-params)
  633. (member "table" result-params))
  634. (not (listp result)))
  635. (list (list result)) result)))
  636. ;; If non-empty result and :file then write to :file.
  637. (when (cdr (assoc :file params))
  638. (when result
  639. (with-temp-file (cdr (assoc :file params))
  640. (insert
  641. (org-babel-format-result
  642. result (cdr (assoc :sep (nth 2 info)))))))
  643. (setq result (cdr (assoc :file params))))
  644. ;; Possibly perform post process provided its appropriate.
  645. (when (cdr (assoc :post params))
  646. (let ((*this* (if (cdr (assoc :file params))
  647. (org-babel-result-to-file
  648. (cdr (assoc :file params))
  649. (when (assoc :file-desc params)
  650. (or (cdr (assoc :file-desc params))
  651. result)))
  652. result)))
  653. (setq result (org-babel-ref-resolve
  654. (cdr (assoc :post params))))
  655. (when (cdr (assoc :file params))
  656. (setq result-params
  657. (remove "file" result-params)))))
  658. (org-babel-insert-result
  659. result result-params info new-hash indent lang))
  660. (run-hooks 'org-babel-after-execute-hook)
  661. result)
  662. (setq call-process-region
  663. 'org-babel-call-process-region-original)))))))))
  664. (defun org-babel-expand-body:generic (body params &optional var-lines)
  665. "Expand BODY with PARAMS.
  666. Expand a block of code with org-babel according to its header
  667. arguments. This generic implementation of body expansion is
  668. called for languages which have not defined their own specific
  669. org-babel-expand-body:lang function."
  670. (let ((pro (cdr (assoc :prologue params)))
  671. (epi (cdr (assoc :epilogue params))))
  672. (mapconcat #'identity
  673. (append (when pro (list pro))
  674. var-lines
  675. (list body)
  676. (when epi (list epi)))
  677. "\n")))
  678. ;;;###autoload
  679. (defun org-babel-expand-src-block (&optional arg info params)
  680. "Expand the current source code block.
  681. Expand according to the source code block's header
  682. arguments and pop open the results in a preview buffer."
  683. (interactive)
  684. (let* ((info (or info (org-babel-get-src-block-info)))
  685. (lang (nth 0 info))
  686. (params (setf (nth 2 info)
  687. (sort (org-babel-merge-params (nth 2 info) params)
  688. (lambda (el1 el2) (string< (symbol-name (car el1))
  689. (symbol-name (car el2)))))))
  690. (body (setf (nth 1 info)
  691. (if (org-babel-noweb-p params :eval)
  692. (org-babel-expand-noweb-references info) (nth 1 info))))
  693. (expand-cmd (intern (concat "org-babel-expand-body:" lang)))
  694. (assignments-cmd (intern (concat "org-babel-variable-assignments:"
  695. lang)))
  696. (expanded
  697. (if (fboundp expand-cmd) (funcall expand-cmd body params)
  698. (org-babel-expand-body:generic
  699. body params (and (fboundp assignments-cmd)
  700. (funcall assignments-cmd params))))))
  701. (if (org-called-interactively-p 'any)
  702. (org-edit-src-code
  703. expanded (concat "*Org-Babel Preview " (buffer-name) "[ " lang " ]*"))
  704. expanded)))
  705. (defun org-babel-edit-distance (s1 s2)
  706. "Return the edit (levenshtein) distance between strings S1 S2."
  707. (let* ((l1 (length s1))
  708. (l2 (length s2))
  709. (dist (vconcat (mapcar (lambda (_) (make-vector (1+ l2) nil))
  710. (number-sequence 1 (1+ l1)))))
  711. (in (lambda (i j) (aref (aref dist i) j))))
  712. (setf (aref (aref dist 0) 0) 0)
  713. (dolist (j (number-sequence 1 l2))
  714. (setf (aref (aref dist 0) j) j))
  715. (dolist (i (number-sequence 1 l1))
  716. (setf (aref (aref dist i) 0) i)
  717. (dolist (j (number-sequence 1 l2))
  718. (setf (aref (aref dist i) j)
  719. (min
  720. (1+ (funcall in (1- i) j))
  721. (1+ (funcall in i (1- j)))
  722. (+ (if (equal (aref s1 (1- i)) (aref s2 (1- j))) 0 1)
  723. (funcall in (1- i) (1- j)))))))
  724. (funcall in l1 l2)))
  725. (defun org-babel-combine-header-arg-lists (original &rest others)
  726. "Combine a number of lists of header argument names and arguments."
  727. (let ((results (copy-sequence original)))
  728. (dolist (new-list others)
  729. (dolist (arg-pair new-list)
  730. (let ((header (car arg-pair))
  731. (args (cdr arg-pair)))
  732. (setq results
  733. (cons arg-pair (cl-remove-if
  734. (lambda (pair) (equal header (car pair)))
  735. results))))))
  736. results))
  737. ;;;###autoload
  738. (defun org-babel-check-src-block ()
  739. "Check for misspelled header arguments in the current code block."
  740. (interactive)
  741. ;; TODO: report malformed code block
  742. ;; TODO: report incompatible combinations of header arguments
  743. ;; TODO: report uninitialized variables
  744. (let ((too-close 2) ;; <- control closeness to report potential match
  745. (names (mapcar #'symbol-name org-babel-header-arg-names)))
  746. (dolist (header (mapcar (lambda (arg) (substring (symbol-name (car arg)) 1))
  747. (and (org-babel-where-is-src-block-head)
  748. (org-babel-parse-header-arguments
  749. (org-no-properties
  750. (match-string 4))))))
  751. (dolist (name names)
  752. (when (and (not (string= header name))
  753. (<= (org-babel-edit-distance header name) too-close)
  754. (not (member header names)))
  755. (error "Supplied header \"%S\" is suspiciously close to \"%S\""
  756. header name))))
  757. (message "No suspicious header arguments found.")))
  758. ;;;###autoload
  759. (defun org-babel-insert-header-arg (&optional header-arg value)
  760. "Insert a header argument selecting from lists of common args and values."
  761. (interactive)
  762. (let* ((info (org-babel-get-src-block-info 'light))
  763. (lang (car info))
  764. (begin (nth 6 info))
  765. (lang-headers (intern (concat "org-babel-header-args:" lang)))
  766. (headers (org-babel-combine-header-arg-lists
  767. org-babel-common-header-args-w-values
  768. (when (boundp lang-headers) (eval lang-headers))))
  769. (header-arg (or header-arg
  770. (org-icompleting-read
  771. "Header Arg: "
  772. (mapcar
  773. (lambda (header-spec) (symbol-name (car header-spec)))
  774. headers))))
  775. (vals (cdr (assoc (intern header-arg) headers)))
  776. (value (or value
  777. (cond
  778. ((eq vals :any)
  779. (read-from-minibuffer "value: "))
  780. ((listp vals)
  781. (mapconcat
  782. (lambda (group)
  783. (let ((arg (org-icompleting-read
  784. "Value: "
  785. (cons "default"
  786. (mapcar #'symbol-name group)))))
  787. (if (and arg (not (string= "default" arg)))
  788. (concat arg " ")
  789. "")))
  790. vals ""))))))
  791. (save-excursion
  792. (goto-char begin)
  793. (goto-char (point-at-eol))
  794. (unless (= (char-before (point)) ?\ ) (insert " "))
  795. (insert ":" header-arg) (when value (insert " " value)))))
  796. ;; Add support for completing-read insertion of header arguments after ":"
  797. (defun org-babel-header-arg-expand ()
  798. "Call `org-babel-enter-header-arg-w-completion' in appropriate contexts."
  799. (when (and (equal (char-before) ?\:) (org-babel-where-is-src-block-head))
  800. (org-babel-enter-header-arg-w-completion (match-string 2))))
  801. (defun org-babel-enter-header-arg-w-completion (&optional lang)
  802. "Insert header argument appropriate for LANG with completion."
  803. (let* ((lang-headers-var (intern (concat "org-babel-header-args:" lang)))
  804. (lang-headers (when (boundp lang-headers-var) (eval lang-headers-var)))
  805. (headers-w-values (org-babel-combine-header-arg-lists
  806. org-babel-common-header-args-w-values lang-headers))
  807. (headers (mapcar #'symbol-name (mapcar #'car headers-w-values)))
  808. (header (org-completing-read "Header Arg: " headers))
  809. (args (cdr (assoc (intern header) headers-w-values)))
  810. (arg (when (and args (listp args))
  811. (org-completing-read
  812. (format "%s: " header)
  813. (mapcar #'symbol-name (apply #'append args))))))
  814. (insert (concat header " " (or arg "")))
  815. (cons header arg)))
  816. (add-hook 'org-tab-first-hook 'org-babel-header-arg-expand)
  817. ;;;###autoload
  818. (defun org-babel-load-in-session (&optional arg info)
  819. "Load the body of the current source-code block.
  820. Evaluate the header arguments for the source block before
  821. entering the session. After loading the body this pops open the
  822. session."
  823. (interactive)
  824. (let* ((info (or info (org-babel-get-src-block-info)))
  825. (lang (nth 0 info))
  826. (params (nth 2 info))
  827. (body (if (not info)
  828. (user-error "No src code block at point")
  829. (setf (nth 1 info)
  830. (if (org-babel-noweb-p params :eval)
  831. (org-babel-expand-noweb-references info)
  832. (nth 1 info)))))
  833. (session (cdr (assoc :session params)))
  834. (dir (cdr (assoc :dir params)))
  835. (default-directory
  836. (or (and dir (file-name-as-directory dir)) default-directory))
  837. (cmd (intern (concat "org-babel-load-session:" lang))))
  838. (unless (fboundp cmd)
  839. (error "No org-babel-load-session function for %s!" lang))
  840. (pop-to-buffer (funcall cmd session body params))
  841. (end-of-line 1)))
  842. ;;;###autoload
  843. (defun org-babel-initiate-session (&optional arg info)
  844. "Initiate session for current code block.
  845. If called with a prefix argument then resolve any variable
  846. references in the header arguments and assign these variables in
  847. the session. Copy the body of the code block to the kill ring."
  848. (interactive "P")
  849. (let* ((info (or info (org-babel-get-src-block-info (not arg))))
  850. (lang (nth 0 info))
  851. (body (nth 1 info))
  852. (params (nth 2 info))
  853. (session (cdr (assoc :session params)))
  854. (dir (cdr (assoc :dir params)))
  855. (default-directory
  856. (or (and dir (file-name-as-directory dir)) default-directory))
  857. (init-cmd (intern (format "org-babel-%s-initiate-session" lang)))
  858. (prep-cmd (intern (concat "org-babel-prep-session:" lang))))
  859. (if (and (stringp session) (string= session "none"))
  860. (error "This block is not using a session!"))
  861. (unless (fboundp init-cmd)
  862. (error "No org-babel-initiate-session function for %s!" lang))
  863. (with-temp-buffer (insert (org-babel-trim body))
  864. (copy-region-as-kill (point-min) (point-max)))
  865. (when arg
  866. (unless (fboundp prep-cmd)
  867. (error "No org-babel-prep-session function for %s!" lang))
  868. (funcall prep-cmd session params))
  869. (funcall init-cmd session params)))
  870. ;;;###autoload
  871. (defun org-babel-switch-to-session (&optional arg info)
  872. "Switch to the session of the current code block.
  873. Uses `org-babel-initiate-session' to start the session. If called
  874. with a prefix argument then this is passed on to
  875. `org-babel-initiate-session'."
  876. (interactive "P")
  877. (pop-to-buffer (org-babel-initiate-session arg info))
  878. (end-of-line 1))
  879. (defalias 'org-babel-pop-to-session 'org-babel-switch-to-session)
  880. (defvar org-src-window-setup)
  881. ;;;###autoload
  882. (defun org-babel-switch-to-session-with-code (&optional arg info)
  883. "Switch to code buffer and display session."
  884. (interactive "P")
  885. (let ((swap-windows
  886. (lambda ()
  887. (let ((other-window-buffer (window-buffer (next-window))))
  888. (set-window-buffer (next-window) (current-buffer))
  889. (set-window-buffer (selected-window) other-window-buffer))
  890. (other-window 1)))
  891. (info (org-babel-get-src-block-info))
  892. (org-src-window-setup 'reorganize-frame))
  893. (save-excursion
  894. (org-babel-switch-to-session arg info))
  895. (org-edit-src-code)
  896. (funcall swap-windows)))
  897. ;;;###autoload
  898. (defmacro org-babel-do-in-edit-buffer (&rest body)
  899. "Evaluate BODY in edit buffer if there is a code block at point.
  900. Return t if a code block was found at point, nil otherwise."
  901. `(let ((org-src-window-setup 'switch-invisibly))
  902. (when (and (org-babel-where-is-src-block-head)
  903. (org-edit-src-code))
  904. (unwind-protect (progn ,@body)
  905. (org-edit-src-exit))
  906. t)))
  907. (def-edebug-spec org-babel-do-in-edit-buffer (body))
  908. (defun org-babel-do-key-sequence-in-edit-buffer (key)
  909. "Read key sequence and execute the command in edit buffer.
  910. Enter a key sequence to be executed in the language major-mode
  911. edit buffer. For example, TAB will alter the contents of the
  912. Org-mode code block according to the effect of TAB in the
  913. language major-mode buffer. For languages that support
  914. interactive sessions, this can be used to send code from the Org
  915. buffer to the session for evaluation using the native major-mode
  916. evaluation mechanisms."
  917. (interactive "kEnter key-sequence to execute in edit buffer: ")
  918. (org-babel-do-in-edit-buffer
  919. (call-interactively
  920. (key-binding (or key (read-key-sequence nil))))))
  921. (defvar org-bracket-link-regexp)
  922. (defun org-babel-active-location-p ()
  923. (memq (org-element-type (save-match-data (org-element-context)))
  924. '(babel-call inline-babel-call inline-src-block src-block)))
  925. ;;;###autoload
  926. (defun org-babel-open-src-block-result (&optional re-run)
  927. "If `point' is on a src block then open the results of the
  928. source code block, otherwise return nil. With optional prefix
  929. argument RE-RUN the source-code block is evaluated even if
  930. results already exist."
  931. (interactive "P")
  932. (let ((info (org-babel-get-src-block-info 'light)))
  933. (when info
  934. (save-excursion
  935. ;; go to the results, if there aren't any then run the block
  936. (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
  937. (progn (org-babel-execute-src-block)
  938. (org-babel-where-is-src-block-result))))
  939. (end-of-line 1)
  940. (while (looking-at "[\n\r\t\f ]") (forward-char 1))
  941. ;; open the results
  942. (if (looking-at org-bracket-link-regexp)
  943. ;; file results
  944. (org-open-at-point)
  945. (let ((r (org-babel-format-result
  946. (org-babel-read-result) (cdr (assoc :sep (nth 2 info))))))
  947. (pop-to-buffer (get-buffer-create "*Org-Babel Results*"))
  948. (delete-region (point-min) (point-max))
  949. (insert r)))
  950. t))))
  951. ;;;###autoload
  952. (defmacro org-babel-map-src-blocks (file &rest body)
  953. "Evaluate BODY forms on each source-block in FILE.
  954. If FILE is nil evaluate BODY forms on source blocks in current
  955. buffer. During evaluation of BODY the following local variables
  956. are set relative to the currently matched code block.
  957. full-block ------- string holding the entirety of the code block
  958. beg-block -------- point at the beginning of the code block
  959. end-block -------- point at the end of the matched code block
  960. lang ------------- string holding the language of the code block
  961. beg-lang --------- point at the beginning of the lang
  962. end-lang --------- point at the end of the lang
  963. switches --------- string holding the switches
  964. beg-switches ----- point at the beginning of the switches
  965. end-switches ----- point at the end of the switches
  966. header-args ------ string holding the header-args
  967. beg-header-args -- point at the beginning of the header-args
  968. end-header-args -- point at the end of the header-args
  969. body ------------- string holding the body of the code block
  970. beg-body --------- point at the beginning of the body
  971. end-body --------- point at the end of the body"
  972. (declare (indent 1))
  973. (let ((tempvar (make-symbol "file")))
  974. `(let* ((case-fold-search t)
  975. (,tempvar ,file)
  976. (visited-p (or (null ,tempvar)
  977. (get-file-buffer (expand-file-name ,tempvar))))
  978. (point (point)) to-be-removed)
  979. (save-window-excursion
  980. (when ,tempvar (find-file ,tempvar))
  981. (setq to-be-removed (current-buffer))
  982. (goto-char (point-min))
  983. (while (re-search-forward org-babel-src-block-regexp nil t)
  984. (when (org-babel-active-location-p)
  985. (goto-char (match-beginning 0))
  986. (let ((full-block (match-string 0))
  987. (beg-block (match-beginning 0))
  988. (end-block (match-end 0))
  989. (lang (match-string 2))
  990. (beg-lang (match-beginning 2))
  991. (end-lang (match-end 2))
  992. (switches (match-string 3))
  993. (beg-switches (match-beginning 3))
  994. (end-switches (match-end 3))
  995. (header-args (match-string 4))
  996. (beg-header-args (match-beginning 4))
  997. (end-header-args (match-end 4))
  998. (body (match-string 5))
  999. (beg-body (match-beginning 5))
  1000. (end-body (match-end 5)))
  1001. ,@body
  1002. (goto-char end-block)))))
  1003. (unless visited-p (kill-buffer to-be-removed))
  1004. (goto-char point))))
  1005. (def-edebug-spec org-babel-map-src-blocks (form body))
  1006. ;;;###autoload
  1007. (defmacro org-babel-map-inline-src-blocks (file &rest body)
  1008. "Evaluate BODY forms on each inline source-block in FILE.
  1009. If FILE is nil evaluate BODY forms on source blocks in current
  1010. buffer."
  1011. (declare (indent 1))
  1012. (let ((tempvar (make-symbol "file")))
  1013. `(let* ((case-fold-search t)
  1014. (,tempvar ,file)
  1015. (visited-p (or (null ,tempvar)
  1016. (get-file-buffer (expand-file-name ,tempvar))))
  1017. (point (point)) to-be-removed)
  1018. (save-window-excursion
  1019. (when ,tempvar (find-file ,tempvar))
  1020. (setq to-be-removed (current-buffer))
  1021. (goto-char (point-min))
  1022. (while (re-search-forward org-babel-inline-src-block-regexp nil t)
  1023. (when (org-babel-active-location-p)
  1024. (goto-char (match-beginning 1))
  1025. (save-match-data ,@body))
  1026. (goto-char (match-end 0))))
  1027. (unless visited-p (kill-buffer to-be-removed))
  1028. (goto-char point))))
  1029. (def-edebug-spec org-babel-map-inline-src-blocks (form body))
  1030. (defvar org-babel-lob-one-liner-regexp)
  1031. ;;;###autoload
  1032. (defmacro org-babel-map-call-lines (file &rest body)
  1033. "Evaluate BODY forms on each call line in FILE.
  1034. If FILE is nil evaluate BODY forms on source blocks in current
  1035. buffer."
  1036. (declare (indent 1))
  1037. (let ((tempvar (make-symbol "file")))
  1038. `(let* ((,tempvar ,file)
  1039. (visited-p (or (null ,tempvar)
  1040. (get-file-buffer (expand-file-name ,tempvar))))
  1041. (point (point)) to-be-removed)
  1042. (save-window-excursion
  1043. (when ,tempvar (find-file ,tempvar))
  1044. (setq to-be-removed (current-buffer))
  1045. (goto-char (point-min))
  1046. (while (re-search-forward org-babel-lob-one-liner-regexp nil t)
  1047. (when (org-babel-active-location-p)
  1048. (goto-char (match-beginning 1))
  1049. (save-match-data ,@body))
  1050. (goto-char (match-end 0))))
  1051. (unless visited-p (kill-buffer to-be-removed))
  1052. (goto-char point))))
  1053. (def-edebug-spec org-babel-map-call-lines (form body))
  1054. ;;;###autoload
  1055. (defmacro org-babel-map-executables (file &rest body)
  1056. (declare (indent 1))
  1057. (let ((tempvar (make-symbol "file"))
  1058. (rx (make-symbol "rx")))
  1059. `(let* ((,tempvar ,file)
  1060. (,rx (concat "\\(" org-babel-src-block-regexp
  1061. "\\|" org-babel-inline-src-block-regexp
  1062. "\\|" org-babel-lob-one-liner-regexp "\\)"))
  1063. (visited-p (or (null ,tempvar)
  1064. (get-file-buffer (expand-file-name ,tempvar))))
  1065. (point (point)) to-be-removed)
  1066. (save-window-excursion
  1067. (when ,tempvar (find-file ,tempvar))
  1068. (setq to-be-removed (current-buffer))
  1069. (goto-char (point-min))
  1070. (while (re-search-forward ,rx nil t)
  1071. (when (org-babel-active-location-p)
  1072. (goto-char (match-beginning 1))
  1073. (when (looking-at org-babel-inline-src-block-regexp)
  1074. (forward-char 1))
  1075. (save-match-data ,@body))
  1076. (goto-char (match-end 0))))
  1077. (unless visited-p (kill-buffer to-be-removed))
  1078. (goto-char point))))
  1079. (def-edebug-spec org-babel-map-executables (form body))
  1080. ;;;###autoload
  1081. (defun org-babel-execute-buffer (&optional arg)
  1082. "Execute source code blocks in a buffer.
  1083. Call `org-babel-execute-src-block' on every source block in
  1084. the current buffer."
  1085. (interactive "P")
  1086. (org-babel-eval-wipe-error-buffer)
  1087. (org-save-outline-visibility t
  1088. (org-babel-map-executables nil
  1089. (if (looking-at org-babel-lob-one-liner-regexp)
  1090. (org-babel-lob-execute-maybe)
  1091. (org-babel-execute-src-block arg)))))
  1092. ;;;###autoload
  1093. (defun org-babel-execute-subtree (&optional arg)
  1094. "Execute source code blocks in a subtree.
  1095. Call `org-babel-execute-src-block' on every source block in
  1096. the current subtree."
  1097. (interactive "P")
  1098. (save-restriction
  1099. (save-excursion
  1100. (org-narrow-to-subtree)
  1101. (org-babel-execute-buffer arg)
  1102. (widen))))
  1103. ;;;###autoload
  1104. (defun org-babel-sha1-hash (&optional info)
  1105. "Generate an sha1 hash based on the value of info."
  1106. (interactive)
  1107. (let ((print-level nil)
  1108. (info (or info (org-babel-get-src-block-info))))
  1109. (setf (nth 2 info)
  1110. (sort (copy-sequence (nth 2 info))
  1111. (lambda (a b) (string< (car a) (car b)))))
  1112. (let* ((rm (lambda (lst)
  1113. (dolist (p '("replace" "silent" "none"
  1114. "append" "prepend"))
  1115. (setq lst (remove p lst)))
  1116. lst))
  1117. (norm (lambda (arg)
  1118. (let ((v (if (and (listp (cdr arg)) (null (cddr arg)))
  1119. (copy-sequence (cdr arg))
  1120. (cdr arg))))
  1121. (when (and v (not (and (sequencep v)
  1122. (not (consp v))
  1123. (= (length v) 0))))
  1124. (cond
  1125. ((and (listp v) ; lists are sorted
  1126. (member (car arg) '(:result-params)))
  1127. (sort (funcall rm v) #'string<))
  1128. ((and (stringp v) ; strings are sorted
  1129. (member (car arg) '(:results :exports)))
  1130. (mapconcat #'identity (sort (funcall rm (split-string v))
  1131. #'string<) " "))
  1132. (t v))))))
  1133. ;; expanded body
  1134. (lang (nth 0 info))
  1135. (params (nth 2 info))
  1136. (body (if (org-babel-noweb-p params :eval)
  1137. (org-babel-expand-noweb-references info) (nth 1 info)))
  1138. (expand-cmd (intern (concat "org-babel-expand-body:" lang)))
  1139. (assignments-cmd (intern (concat "org-babel-variable-assignments:"
  1140. lang)))
  1141. (expanded
  1142. (if (fboundp expand-cmd) (funcall expand-cmd body params)
  1143. (org-babel-expand-body:generic
  1144. body params (and (fboundp assignments-cmd)
  1145. (funcall assignments-cmd params))))))
  1146. (let* ((it (format "%s-%s"
  1147. (mapconcat
  1148. #'identity
  1149. (delq nil (mapcar (lambda (arg)
  1150. (let ((normalized (funcall norm arg)))
  1151. (when normalized
  1152. (format "%S" normalized))))
  1153. (nth 2 info))) ":")
  1154. expanded))
  1155. (hash (sha1 it)))
  1156. (when (org-called-interactively-p 'interactive) (message hash))
  1157. hash))))
  1158. (defun org-babel-current-result-hash (&optional info)
  1159. "Return the current in-buffer hash."
  1160. (org-babel-where-is-src-block-result nil info)
  1161. (org-no-properties (match-string 5)))
  1162. (defun org-babel-set-current-result-hash (hash info)
  1163. "Set the current in-buffer hash to HASH."
  1164. (org-babel-where-is-src-block-result nil info)
  1165. (save-excursion (goto-char (match-beginning 5))
  1166. (mapc #'delete-overlay (overlays-at (point)))
  1167. (forward-char org-babel-hash-show)
  1168. (mapc #'delete-overlay (overlays-at (point)))
  1169. (replace-match hash nil nil nil 5)
  1170. (goto-char (point-at-bol))
  1171. (org-babel-hide-hash)))
  1172. (defun org-babel-hide-hash ()
  1173. "Hide the hash in the current results line.
  1174. Only the initial `org-babel-hash-show' characters of the hash
  1175. will remain visible."
  1176. (add-to-invisibility-spec '(org-babel-hide-hash . t))
  1177. (save-excursion
  1178. (when (and (re-search-forward org-babel-result-regexp nil t)
  1179. (match-string 5))
  1180. (let* ((start (match-beginning 5))
  1181. (hide-start (+ org-babel-hash-show start))
  1182. (end (match-end 5))
  1183. (hash (match-string 5))
  1184. ov1 ov2)
  1185. (setq ov1 (make-overlay start hide-start))
  1186. (setq ov2 (make-overlay hide-start end))
  1187. (overlay-put ov2 'invisible 'org-babel-hide-hash)
  1188. (overlay-put ov1 'babel-hash hash)))))
  1189. (defun org-babel-hide-all-hashes ()
  1190. "Hide the hash in the current buffer.
  1191. Only the initial `org-babel-hash-show' characters of each hash
  1192. will remain visible. This function should be called as part of
  1193. the `org-mode-hook'."
  1194. (save-excursion
  1195. (while (and (not org-babel-hash-show-time)
  1196. (re-search-forward org-babel-result-regexp nil t))
  1197. (goto-char (match-beginning 0))
  1198. (org-babel-hide-hash)
  1199. (goto-char (match-end 0)))))
  1200. (add-hook 'org-mode-hook 'org-babel-hide-all-hashes)
  1201. (defun org-babel-hash-at-point (&optional point)
  1202. "Return the value of the hash at POINT.
  1203. \\<org-mode-map>\
  1204. The hash is also added as the last element of the kill ring.
  1205. This can be called with \\[org-ctrl-c-ctrl-c]."
  1206. (interactive)
  1207. (let ((hash (car (delq nil (mapcar
  1208. (lambda (ol) (overlay-get ol 'babel-hash))
  1209. (overlays-at (or point (point))))))))
  1210. (when hash (kill-new hash) (message hash))))
  1211. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
  1212. (defun org-babel-result-hide-spec ()
  1213. "Hide portions of results lines.
  1214. Add `org-babel-hide-result' as an invisibility spec for hiding
  1215. portions of results lines."
  1216. (add-to-invisibility-spec '(org-babel-hide-result . t)))
  1217. (add-hook 'org-mode-hook 'org-babel-result-hide-spec)
  1218. (defvar org-babel-hide-result-overlays nil
  1219. "Overlays hiding results.")
  1220. (defun org-babel-result-hide-all ()
  1221. "Fold all results in the current buffer."
  1222. (interactive)
  1223. (org-babel-show-result-all)
  1224. (save-excursion
  1225. (while (re-search-forward org-babel-result-regexp nil t)
  1226. (save-excursion (goto-char (match-beginning 0))
  1227. (org-babel-hide-result-toggle-maybe)))))
  1228. (defun org-babel-show-result-all ()
  1229. "Unfold all results in the current buffer."
  1230. (mapc 'delete-overlay org-babel-hide-result-overlays)
  1231. (setq org-babel-hide-result-overlays nil))
  1232. ;;;###autoload
  1233. (defun org-babel-hide-result-toggle-maybe ()
  1234. "Toggle visibility of result at point."
  1235. (interactive)
  1236. (let ((case-fold-search t))
  1237. (if (save-excursion
  1238. (beginning-of-line 1)
  1239. (looking-at org-babel-result-regexp))
  1240. (progn (org-babel-hide-result-toggle)
  1241. t) ;; to signal that we took action
  1242. nil))) ;; to signal that we did not
  1243. (defun org-babel-hide-result-toggle (&optional force)
  1244. "Toggle the visibility of the current result."
  1245. (interactive)
  1246. (save-excursion
  1247. (beginning-of-line)
  1248. (if (re-search-forward org-babel-result-regexp nil t)
  1249. (let ((start (progn (beginning-of-line 2) (- (point) 1)))
  1250. (end (progn
  1251. (while (looking-at org-babel-multi-line-header-regexp)
  1252. (forward-line 1))
  1253. (goto-char (- (org-babel-result-end) 1)) (point)))
  1254. ov)
  1255. (if (memq t (mapcar (lambda (overlay)
  1256. (eq (overlay-get overlay 'invisible)
  1257. 'org-babel-hide-result))
  1258. (overlays-at start)))
  1259. (if (or (not force) (eq force 'off))
  1260. (mapc (lambda (ov)
  1261. (when (member ov org-babel-hide-result-overlays)
  1262. (setq org-babel-hide-result-overlays
  1263. (delq ov org-babel-hide-result-overlays)))
  1264. (when (eq (overlay-get ov 'invisible)
  1265. 'org-babel-hide-result)
  1266. (delete-overlay ov)))
  1267. (overlays-at start)))
  1268. (setq ov (make-overlay start end))
  1269. (overlay-put ov 'invisible 'org-babel-hide-result)
  1270. ;; make the block accessible to isearch
  1271. (overlay-put
  1272. ov 'isearch-open-invisible
  1273. (lambda (ov)
  1274. (when (member ov org-babel-hide-result-overlays)
  1275. (setq org-babel-hide-result-overlays
  1276. (delq ov org-babel-hide-result-overlays)))
  1277. (when (eq (overlay-get ov 'invisible)
  1278. 'org-babel-hide-result)
  1279. (delete-overlay ov))))
  1280. (push ov org-babel-hide-result-overlays)))
  1281. (error "Not looking at a result line"))))
  1282. ;; org-tab-after-check-for-cycling-hook
  1283. (add-hook 'org-tab-first-hook 'org-babel-hide-result-toggle-maybe)
  1284. ;; Remove overlays when changing major mode
  1285. (add-hook 'org-mode-hook
  1286. (lambda () (org-add-hook 'change-major-mode-hook
  1287. 'org-babel-show-result-all 'append 'local)))
  1288. (defvar org-file-properties)
  1289. (defun org-babel-params-from-properties (&optional lang)
  1290. "Retrieve parameters specified as properties.
  1291. Return a list of association lists of source block params
  1292. specified in the properties of the current outline entry."
  1293. (save-match-data
  1294. (list
  1295. ;; DEPRECATED header arguments specified as separate property at
  1296. ;; point of definition.
  1297. (org-babel-parse-multiple-vars
  1298. (delq nil
  1299. (mapcar
  1300. (lambda (header)
  1301. (let* ((arg (symbol-name (car header)))
  1302. (val (org-entry-get (point) arg t)))
  1303. (and val
  1304. (cons (intern (concat ":" arg))
  1305. (org-babel-read val)))))
  1306. (org-babel-combine-header-arg-lists
  1307. org-babel-common-header-args-w-values
  1308. (let ((sym (intern (concat "org-babel-header-args:" lang))))
  1309. (and (boundp sym) (symbol-value sym)))))))
  1310. ;; header arguments specified with the header-args property at
  1311. ;; point of call.
  1312. (org-babel-parse-header-arguments
  1313. (org-entry-get org-babel-current-src-block-location
  1314. "header-args"
  1315. 'inherit))
  1316. (and lang ; language-specific header arguments at point of call
  1317. (org-babel-parse-header-arguments
  1318. (org-entry-get org-babel-current-src-block-location
  1319. (concat "header-args:" lang)
  1320. 'inherit))))))
  1321. (defvar org-src-preserve-indentation) ;; declare defcustom from org-src
  1322. (defun org-babel-parse-src-block-match ()
  1323. "Parse the results from a match of the `org-babel-src-block-regexp'."
  1324. (let* ((block-indentation (string-width (match-string 1)))
  1325. (lang (org-match-string-no-properties 2))
  1326. (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
  1327. (switches (match-string 3))
  1328. (body (let* ((body (org-match-string-no-properties 5))
  1329. (sub-length (- (length body) 1)))
  1330. (if (and (> sub-length 0)
  1331. (string= "\n" (substring body sub-length)))
  1332. (substring body 0 sub-length)
  1333. (or body ""))))
  1334. (preserve-indentation (or org-src-preserve-indentation
  1335. (save-match-data
  1336. (string-match "-i\\>" switches)))))
  1337. (list lang
  1338. ;; get block body less properties, protective commas, and indentation
  1339. (with-temp-buffer
  1340. (save-match-data
  1341. (insert (org-unescape-code-in-string body))
  1342. (unless preserve-indentation (org-do-remove-indentation))
  1343. (buffer-string)))
  1344. (apply #'org-babel-merge-params
  1345. org-babel-default-header-args
  1346. (when (boundp lang-headers) (eval lang-headers))
  1347. (append
  1348. (org-babel-params-from-properties lang)
  1349. (list (org-babel-parse-header-arguments
  1350. (org-no-properties (or (match-string 4) ""))))))
  1351. switches
  1352. block-indentation)))
  1353. (defun org-babel-parse-inline-src-block-match ()
  1354. "Parse the results from a match of the `org-babel-inline-src-block-regexp'."
  1355. (let* ((lang (org-no-properties (match-string 2)))
  1356. (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
  1357. (list lang
  1358. (org-unescape-code-in-string (org-no-properties (match-string 5)))
  1359. (apply #'org-babel-merge-params
  1360. org-babel-default-inline-header-args
  1361. (if (boundp lang-headers) (eval lang-headers) nil)
  1362. (append
  1363. (org-babel-params-from-properties lang)
  1364. (list (org-babel-parse-header-arguments
  1365. (org-no-properties (or (match-string 4) ""))))))
  1366. nil)))
  1367. (defun org-babel-balanced-split (string alts)
  1368. "Split STRING on instances of ALTS.
  1369. ALTS is a cons of two character options where each option may be
  1370. either the numeric code of a single character or a list of
  1371. character alternatives. For example to split on balanced
  1372. instances of \"[ \t]:\" set ALTS to ((32 9) . 58)."
  1373. (let* ((matches (lambda (ch spec) (if (listp spec) (member ch spec) (equal spec ch))))
  1374. (matched (lambda (ch last)
  1375. (if (consp alts)
  1376. (and (funcall matches ch (cdr alts))
  1377. (funcall matches last (car alts)))
  1378. (funcall matches ch alts))))
  1379. (balance 0) (last 0)
  1380. quote partial lst)
  1381. (mapc (lambda (ch) ; split on [], (), "" balanced instances of [ \t]:
  1382. (setq balance (+ balance
  1383. (cond ((or (equal 91 ch) (equal 40 ch)) 1)
  1384. ((or (equal 93 ch) (equal 41 ch)) -1)
  1385. (t 0))))
  1386. (when (and (equal 34 ch) (not (equal 92 last)))
  1387. (setq quote (not quote)))
  1388. (setq partial (cons ch partial))
  1389. (when (and (= balance 0) (not quote) (funcall matched ch last))
  1390. (setq lst (cons (apply #'string (nreverse
  1391. (if (consp alts)
  1392. (cddr partial)
  1393. (cdr partial))))
  1394. lst))
  1395. (setq partial nil))
  1396. (setq last ch))
  1397. (string-to-list string))
  1398. (nreverse (cons (apply #'string (nreverse partial)) lst))))
  1399. (defun org-babel-join-splits-near-ch (ch list)
  1400. "Join splits where \"=\" is on either end of the split."
  1401. (let ((last= (lambda (str) (= ch (aref str (1- (length str))))))
  1402. (first= (lambda (str) (= ch (aref str 0)))))
  1403. (reverse
  1404. (cl-reduce (lambda (acc el)
  1405. (let ((head (car acc)))
  1406. (if (and head (or (funcall last= head) (funcall first= el)))
  1407. (cons (concat head el) (cdr acc))
  1408. (cons el acc))))
  1409. list :initial-value nil))))
  1410. (defun org-babel-parse-header-arguments (arg-string)
  1411. "Parse a string of header arguments returning an alist."
  1412. (when (> (length arg-string) 0)
  1413. (org-babel-parse-multiple-vars
  1414. (delq nil
  1415. (mapcar
  1416. (lambda (arg)
  1417. (if (string-match
  1418. "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)"
  1419. arg)
  1420. (cons (intern (match-string 1 arg))
  1421. (org-babel-read (org-babel-chomp (match-string 2 arg))))
  1422. (cons (intern (org-babel-chomp arg)) nil)))
  1423. (let ((raw (org-babel-balanced-split arg-string '((32 9) . 58))))
  1424. (cons (car raw) (mapcar (lambda (r) (concat ":" r)) (cdr raw)))))))))
  1425. (defun org-babel-parse-multiple-vars (header-arguments)
  1426. "Expand multiple variable assignments behind a single :var keyword.
  1427. This allows expression of multiple variables with one :var as
  1428. shown below.
  1429. #+PROPERTY: var foo=1, bar=2"
  1430. (let (results)
  1431. (mapc (lambda (pair)
  1432. (if (eq (car pair) :var)
  1433. (mapcar (lambda (v) (push (cons :var (org-babel-trim v)) results))
  1434. (org-babel-join-splits-near-ch
  1435. 61 (org-babel-balanced-split (cdr pair) 32)))
  1436. (push pair results)))
  1437. header-arguments)
  1438. (nreverse results)))
  1439. (defun org-babel-process-params (params)
  1440. "Expand variables in PARAMS and add summary parameters."
  1441. (let* ((processed-vars (mapcar (lambda (el)
  1442. (if (consp el)
  1443. el
  1444. (org-babel-ref-parse el)))
  1445. (org-babel--get-vars params)))
  1446. (vars-and-names (if (and (assoc :colname-names params)
  1447. (assoc :rowname-names params))
  1448. (list processed-vars)
  1449. (org-babel-disassemble-tables
  1450. processed-vars
  1451. (cdr (assoc :hlines params))
  1452. (cdr (assoc :colnames params))
  1453. (cdr (assoc :rownames params)))))
  1454. (raw-result (or (cdr (assoc :results params)) ""))
  1455. (result-params (append
  1456. (split-string (if (stringp raw-result)
  1457. raw-result
  1458. (eval raw-result)))
  1459. (cdr (assoc :result-params params)))))
  1460. (append
  1461. (mapcar (lambda (var) (cons :var var)) (car vars-and-names))
  1462. (list
  1463. (cons :colname-names (or (cdr (assoc :colname-names params))
  1464. (cadr vars-and-names)))
  1465. (cons :rowname-names (or (cdr (assoc :rowname-names params))
  1466. (caddr vars-and-names)))
  1467. (cons :result-params result-params)
  1468. (cons :result-type (cond ((member "output" result-params) 'output)
  1469. ((member "value" result-params) 'value)
  1470. (t 'value))))
  1471. (cl-remove-if
  1472. (lambda (x) (memq (car x) '(:colname-names :rowname-names :result-params
  1473. :result-type :var)))
  1474. params))))
  1475. ;; row and column names
  1476. (defun org-babel-del-hlines (table)
  1477. "Remove all `hline's from TABLE."
  1478. (remq 'hline table))
  1479. (defun org-babel-get-colnames (table)
  1480. "Return the column names of TABLE.
  1481. Return a cons cell, the `car' of which contains the TABLE less
  1482. colnames, and the `cdr' of which contains a list of the column
  1483. names."
  1484. (if (equal 'hline (nth 1 table))
  1485. (cons (cddr table) (car table))
  1486. (cons (cdr table) (car table))))
  1487. (defun org-babel-get-rownames (table)
  1488. "Return the row names of TABLE.
  1489. Return a cons cell, the `car' of which contains the TABLE less
  1490. rownames, and the `cdr' of which contains a list of the rownames.
  1491. Note: this function removes any hlines in TABLE."
  1492. (let* ((table (org-babel-del-hlines table))
  1493. (rownames (funcall (lambda ()
  1494. (let ((tp table))
  1495. (mapcar
  1496. (lambda (row)
  1497. (prog1
  1498. (pop (car tp))
  1499. (setq tp (cdr tp))))
  1500. table))))))
  1501. (cons table rownames)))
  1502. (defun org-babel-put-colnames (table colnames)
  1503. "Add COLNAMES to TABLE if they exist."
  1504. (if colnames (apply 'list colnames 'hline table) table))
  1505. (defun org-babel-put-rownames (table rownames)
  1506. "Add ROWNAMES to TABLE if they exist."
  1507. (if rownames
  1508. (mapcar (lambda (row)
  1509. (if (listp row)
  1510. (cons (or (pop rownames) "") row)
  1511. row)) table)
  1512. table))
  1513. (defun org-babel-pick-name (names selector)
  1514. "Select one out of an alist of row or column names.
  1515. SELECTOR can be either a list of names in which case those names
  1516. will be returned directly, or an index into the list NAMES in
  1517. which case the indexed names will be return."
  1518. (if (listp selector)
  1519. selector
  1520. (when names
  1521. (if (and selector (symbolp selector) (not (equal t selector)))
  1522. (cdr (assoc selector names))
  1523. (if (integerp selector)
  1524. (nth (- selector 1) names)
  1525. (cdr (car (last names))))))))
  1526. (defun org-babel-disassemble-tables (vars hlines colnames rownames)
  1527. "Parse tables for further processing.
  1528. Process the variables in VARS according to the HLINES,
  1529. ROWNAMES and COLNAMES header arguments. Return a list consisting
  1530. of the vars, cnames and rnames."
  1531. (let (cnames rnames)
  1532. (list
  1533. (mapcar
  1534. (lambda (var)
  1535. (when (listp (cdr var))
  1536. (when (and (not (equal colnames "no"))
  1537. (or colnames (and (equal (nth 1 (cdr var)) 'hline)
  1538. (not (member 'hline (cddr (cdr var)))))))
  1539. (let ((both (org-babel-get-colnames (cdr var))))
  1540. (setq cnames (cons (cons (car var) (cdr both))
  1541. cnames))
  1542. (setq var (cons (car var) (car both)))))
  1543. (when (and rownames (not (equal rownames "no")))
  1544. (let ((both (org-babel-get-rownames (cdr var))))
  1545. (setq rnames (cons (cons (car var) (cdr both))
  1546. rnames))
  1547. (setq var (cons (car var) (car both)))))
  1548. (when (and hlines (not (equal hlines "yes")))
  1549. (setq var (cons (car var) (org-babel-del-hlines (cdr var))))))
  1550. var)
  1551. vars)
  1552. (reverse cnames) (reverse rnames))))
  1553. (defun org-babel-reassemble-table (table colnames rownames)
  1554. "Add column and row names to a table.
  1555. Given a TABLE and set of COLNAMES and ROWNAMES add the names
  1556. to the table for reinsertion to org-mode."
  1557. (if (listp table)
  1558. (let ((table (if (and rownames (= (length table) (length rownames)))
  1559. (org-babel-put-rownames table rownames) table)))
  1560. (if (and colnames (listp (car table)) (= (length (car table))
  1561. (length colnames)))
  1562. (org-babel-put-colnames table colnames) table))
  1563. table))
  1564. (defun org-babel-where-is-src-block-head ()
  1565. "Find where the current source block begins.
  1566. Return the point at the beginning of the current source block.
  1567. Specifically at the beginning of the #+BEGIN_SRC line. Also set
  1568. match-data relatively to `org-babel-src-block-regexp', which see.
  1569. If the point is not on a source block then return nil."
  1570. (let ((element (org-element-at-point)))
  1571. (when (eq (org-element-type element) 'src-block)
  1572. (let ((end (org-element-property :end element)))
  1573. (org-with-wide-buffer
  1574. ;; Ensure point is not on a blank line after the block.
  1575. (beginning-of-line)
  1576. (skip-chars-forward " \r\t\n" end)
  1577. (when (< (point) end)
  1578. (prog1 (goto-char (org-element-property :post-affiliated element))
  1579. (looking-at org-babel-src-block-regexp))))))))
  1580. ;;;###autoload
  1581. (defun org-babel-goto-src-block-head ()
  1582. "Go to the beginning of the current code block."
  1583. (interactive)
  1584. (let ((head (org-babel-where-is-src-block-head)))
  1585. (if head (goto-char head) (error "Not currently in a code block"))))
  1586. ;;;###autoload
  1587. (defun org-babel-goto-named-src-block (name)
  1588. "Go to a named source-code block."
  1589. (interactive
  1590. (let ((completion-ignore-case t)
  1591. (case-fold-search t)
  1592. (under-point (thing-at-point 'line)))
  1593. (list (org-icompleting-read
  1594. "source-block name: " (org-babel-src-block-names) nil t
  1595. (cond
  1596. ;; noweb
  1597. ((string-match (org-babel-noweb-wrap) under-point)
  1598. (let ((block-name (match-string 1 under-point)))
  1599. (string-match "[^(]*" block-name)
  1600. (match-string 0 block-name)))
  1601. ;; #+call:
  1602. ((string-match org-babel-lob-one-liner-regexp under-point)
  1603. (let ((source-info (car (org-babel-lob-get-info))))
  1604. (if (string-match "^\\([^\\[]+?\\)\\(\\[.*\\]\\)?(" source-info)
  1605. (let ((source-name (match-string 1 source-info)))
  1606. source-name))))
  1607. ;; #+results:
  1608. ((string-match (concat "#\\+" org-babel-results-keyword
  1609. "\\:\s+\\([^\\(]*\\)") under-point)
  1610. (match-string 1 under-point))
  1611. ;; symbol-at-point
  1612. ((and (thing-at-point 'symbol))
  1613. (org-babel-find-named-block (thing-at-point 'symbol))
  1614. (thing-at-point 'symbol))
  1615. (""))))))
  1616. (let ((point (org-babel-find-named-block name)))
  1617. (if point
  1618. ;; taken from `org-open-at-point'
  1619. (progn (org-mark-ring-push) (goto-char point) (org-show-context))
  1620. (message "source-code block `%s' not found in this buffer" name))))
  1621. (defun org-babel-find-named-block (name)
  1622. "Find a named source-code block.
  1623. Return the location of the source block identified by source
  1624. NAME, or nil if no such block exists. Set match data according
  1625. to `org-babel-named-src-block-regexp'."
  1626. (save-excursion
  1627. (goto-char (point-min))
  1628. (ignore-errors
  1629. (org-next-block 1 nil (org-babel-named-src-block-regexp-for-name name)))))
  1630. (defun org-babel-src-block-names (&optional file)
  1631. "Returns the names of source blocks in FILE or the current buffer."
  1632. (when file (find-file file))
  1633. (save-excursion
  1634. (goto-char (point-min))
  1635. (let ((re (org-babel-named-src-block-regexp-for-name))
  1636. names)
  1637. (while (ignore-errors (org-next-block 1 nil re))
  1638. (push (org-match-string-no-properties 9) names))
  1639. names)))
  1640. ;;;###autoload
  1641. (defun org-babel-goto-named-result (name)
  1642. "Go to a named result."
  1643. (interactive
  1644. (let ((completion-ignore-case t))
  1645. (list (org-icompleting-read "source-block name: "
  1646. (org-babel-result-names) nil t))))
  1647. (let ((point (org-babel-find-named-result name)))
  1648. (if point
  1649. ;; taken from `org-open-at-point'
  1650. (progn (goto-char point) (org-show-context))
  1651. (message "result `%s' not found in this buffer" name))))
  1652. (defun org-babel-find-named-result (name &optional point)
  1653. "Find a named result.
  1654. Return the location of the result named NAME in the current
  1655. buffer or nil if no such result exists."
  1656. (save-excursion
  1657. (let ((case-fold-search t))
  1658. (goto-char (or point (point-min)))
  1659. (catch 'is-a-code-block
  1660. (when (re-search-forward
  1661. (concat org-babel-result-regexp
  1662. "[ \t]" (regexp-quote name) "[ \t]*[\n\f\v\r]") nil t)
  1663. (when (and (string= "name" (downcase (match-string 1)))
  1664. (or (beginning-of-line 1)
  1665. (looking-at org-babel-src-block-regexp)
  1666. (looking-at org-babel-multi-line-header-regexp)
  1667. (looking-at org-babel-lob-one-liner-regexp)))
  1668. (throw 'is-a-code-block (org-babel-find-named-result name (point))))
  1669. (beginning-of-line 0) (point))))))
  1670. (defun org-babel-result-names (&optional file)
  1671. "Returns the names of results in FILE or the current buffer."
  1672. (save-excursion
  1673. (when file (find-file file)) (goto-char (point-min))
  1674. (let ((case-fold-search t) names)
  1675. (while (re-search-forward org-babel-result-w-name-regexp nil t)
  1676. (setq names (cons (match-string 4) names)))
  1677. names)))
  1678. ;;;###autoload
  1679. (defun org-babel-next-src-block (&optional arg)
  1680. "Jump to the next source block.
  1681. With optional prefix argument ARG, jump forward ARG many source blocks."
  1682. (interactive "p")
  1683. (org-next-block arg nil org-babel-src-block-regexp))
  1684. ;;;###autoload
  1685. (defun org-babel-previous-src-block (&optional arg)
  1686. "Jump to the previous source block.
  1687. With optional prefix argument ARG, jump backward ARG many source blocks."
  1688. (interactive "p")
  1689. (org-previous-block arg org-babel-src-block-regexp))
  1690. (defvar org-babel-load-languages)
  1691. ;;;###autoload
  1692. (defun org-babel-mark-block ()
  1693. "Mark current src block."
  1694. (interactive)
  1695. (let ((head (org-babel-where-is-src-block-head)))
  1696. (when head
  1697. (save-excursion
  1698. (goto-char head)
  1699. (looking-at org-babel-src-block-regexp))
  1700. (push-mark (match-end 5) nil t)
  1701. (goto-char (match-beginning 5)))))
  1702. (defun org-babel-demarcate-block (&optional arg)
  1703. "Wrap or split the code in the region or on the point.
  1704. When called from inside of a code block the current block is
  1705. split. When called from outside of a code block a new code block
  1706. is created. In both cases if the region is demarcated and if the
  1707. region is not active then the point is demarcated."
  1708. (interactive "P")
  1709. (let* ((info (org-babel-get-src-block-info 'light))
  1710. (start (org-babel-where-is-src-block-head))
  1711. (block (and start (match-string 0)))
  1712. (headers (and start (match-string 4)))
  1713. (stars (concat (make-string (or (org-current-level) 1) ?*) " "))
  1714. (lower-case-p (and block
  1715. (let (case-fold-search)
  1716. (org-string-match-p "#\\+begin_src" block)))))
  1717. (if info
  1718. (mapc
  1719. (lambda (place)
  1720. (save-excursion
  1721. (goto-char place)
  1722. (let ((lang (nth 0 info))
  1723. (indent (make-string (nth 5 info) ? )))
  1724. (when (string-match "^[[:space:]]*$"
  1725. (buffer-substring (point-at-bol)
  1726. (point-at-eol)))
  1727. (delete-region (point-at-bol) (point-at-eol)))
  1728. (insert (concat
  1729. (if (looking-at "^") "" "\n")
  1730. indent (funcall (if lower-case-p 'downcase 'upcase) "#+end_src\n")
  1731. (if arg stars indent) "\n"
  1732. indent (funcall (if lower-case-p 'downcase 'upcase) "#+begin_src ")
  1733. lang
  1734. (if (> (length headers) 1)
  1735. (concat " " headers) headers)
  1736. (if (looking-at "[\n\r]")
  1737. ""
  1738. (concat "\n" (make-string (current-column) ? )))))))
  1739. (move-end-of-line 2))
  1740. (sort (if (org-region-active-p) (list (mark) (point)) (list (point))) #'>))
  1741. (let ((start (point))
  1742. (lang (org-icompleting-read
  1743. "Lang: "
  1744. (mapcar #'symbol-name
  1745. (delete-dups
  1746. (append (mapcar #'car org-babel-load-languages)
  1747. (mapcar (lambda (el) (intern (car el)))
  1748. org-src-lang-modes))))))
  1749. (body (delete-and-extract-region
  1750. (if (org-region-active-p) (mark) (point)) (point))))
  1751. (insert (concat (if (looking-at "^") "" "\n")
  1752. (if arg (concat stars "\n") "")
  1753. (funcall (if lower-case-p 'downcase 'upcase) "#+begin_src ")
  1754. lang "\n"
  1755. body
  1756. (if (or (= (length body) 0)
  1757. (string-match "[\r\n]$" body)) "" "\n")
  1758. (funcall (if lower-case-p 'downcase 'upcase) "#+end_src\n")))
  1759. (goto-char start) (move-end-of-line 1)))))
  1760. (defvar org-babel-lob-one-liner-regexp)
  1761. (defun org-babel-where-is-src-block-result (&optional insert info hash indent)
  1762. "Find where the current source block results begin.
  1763. Return the point at the beginning of the result of the current
  1764. source block. Specifically at the beginning of the results line.
  1765. If no result exists for this block then create a results line
  1766. following the source block."
  1767. (save-excursion
  1768. (let* ((case-fold-search t)
  1769. (on-lob-line (save-excursion
  1770. (beginning-of-line 1)
  1771. (looking-at org-babel-lob-one-liner-regexp)))
  1772. (inlinep (when (org-babel-get-inline-src-block-matches)
  1773. (match-end 0)))
  1774. (name (nth 4 (or info (org-babel-get-src-block-info 'light))))
  1775. (head (unless on-lob-line (org-babel-where-is-src-block-head)))
  1776. found beg end)
  1777. (when head (goto-char head))
  1778. (org-with-wide-buffer
  1779. (setq
  1780. found ;; was there a result (before we potentially insert one)
  1781. (or
  1782. inlinep
  1783. (and
  1784. ;; named results:
  1785. ;; - return t if it is found, else return nil
  1786. ;; - if it does not need to be rebuilt, then don't set end
  1787. ;; - if it does need to be rebuilt then do set end
  1788. name (setq beg (org-babel-find-named-result name))
  1789. (prog1 beg
  1790. (when (and hash (not (string= hash (match-string 5))))
  1791. (goto-char beg) (setq end beg) ;; beginning of result
  1792. (forward-line 1)
  1793. (delete-region end (org-babel-result-end)) nil)))
  1794. (and
  1795. ;; unnamed results:
  1796. ;; - return t if it is found, else return nil
  1797. ;; - if it is found, and the hash doesn't match, delete and set end
  1798. (or on-lob-line (re-search-forward "^[ \t]*#\\+end_src" nil t))
  1799. (progn (end-of-line 1)
  1800. (if (eobp) (insert "\n") (forward-char 1))
  1801. (setq end (point))
  1802. (and
  1803. (not name)
  1804. (progn ;; unnamed results line already exists
  1805. (catch 'non-comment
  1806. (while (re-search-forward "[^ \f\t\n\r\v]" nil t)
  1807. (beginning-of-line 1)
  1808. (cond
  1809. ((looking-at (concat org-babel-result-regexp "\n"))
  1810. (throw 'non-comment t))
  1811. ((and (looking-at "^[ \t]*#")
  1812. (not (looking-at
  1813. org-babel-lob-one-liner-regexp)))
  1814. (end-of-line 1))
  1815. (t (throw 'non-comment nil))))))
  1816. (let ((this-hash (match-string 5)))
  1817. (prog1 (point)
  1818. ;; must remove and rebuild if hash!=old-hash
  1819. (if (and hash (not (string= hash this-hash)))
  1820. (progn
  1821. (setq end (point-at-bol))
  1822. (forward-line 1)
  1823. (delete-region end (org-babel-result-end))
  1824. (setq beg end))
  1825. (setq end nil))))))))))
  1826. (if (not (and insert end)) found
  1827. (goto-char end)
  1828. (unless beg
  1829. (if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
  1830. (when (wholenump indent) (indent-to indent))
  1831. (insert (concat
  1832. "#+" org-babel-results-keyword
  1833. (when hash
  1834. (if org-babel-hash-show-time
  1835. (concat
  1836. "["(format-time-string "<%Y-%m-%d %H:%M:%S>")" "hash"]")
  1837. (concat "["hash"]")))
  1838. ":"
  1839. (when name (concat " " name)) "\n"))
  1840. (unless beg (insert "\n") (backward-char))
  1841. (beginning-of-line 0)
  1842. (when hash (org-babel-hide-hash))
  1843. (point)))))
  1844. (defun org-babel-read-element (element)
  1845. "Read ELEMENT into emacs-lisp.
  1846. Return nil if ELEMENT cannot be read."
  1847. (org-with-wide-buffer
  1848. (goto-char (org-element-property :post-affiliated element))
  1849. (pcase (org-element-type element)
  1850. (`fixed-width
  1851. (let ((v (org-babel-trim (org-element-property :value element))))
  1852. (or (org-babel-number-p v) v)))
  1853. (`table (org-babel-read-table))
  1854. (`plain-list (org-babel-read-list))
  1855. ((or `example-block `export-block)
  1856. (org-remove-indentation (org-element-property :value element)))
  1857. (`paragraph
  1858. ;; Treat paragraphs containing a single link specially.
  1859. (skip-chars-forward " \t")
  1860. (if (and (looking-at org-bracket-link-regexp)
  1861. (save-excursion
  1862. (goto-char (match-end 0))
  1863. (skip-chars-forward " \r\t\n")
  1864. (<= (org-element-property :end element)
  1865. (point))))
  1866. (org-babel-read-link)
  1867. (buffer-substring-no-properties
  1868. (org-element-property :contents-begin element)
  1869. (org-element-property :contents-end element))))
  1870. ((or `center-block `quote-block `verse-block `special-block)
  1871. (org-remove-indentation
  1872. (buffer-substring-no-properties
  1873. (org-element-property :contents-begin element)
  1874. (org-element-property :contents-end element))))
  1875. (_ nil))))
  1876. (defvar org-block-regexp)
  1877. (defun org-babel-read-result ()
  1878. "Read the result at `point' into emacs-lisp."
  1879. (let ((case-fold-search t) result-string)
  1880. (cond
  1881. ((org-at-table-p) (org-babel-read-table))
  1882. ((org-at-item-p) (org-babel-read-list))
  1883. ((looking-at org-bracket-link-regexp) (org-babel-read-link))
  1884. ((looking-at org-block-regexp) (org-remove-indentation (match-string 4)))
  1885. ((or (looking-at "^[ \t]*: ") (looking-at "^[ \t]*:$"))
  1886. (setq result-string
  1887. (org-babel-trim
  1888. (mapconcat (lambda (line)
  1889. (or (and (> (length line) 1)
  1890. (string-match "^[ \t]*: ?\\(.+\\)" line)
  1891. (match-string 1 line))
  1892. ""))
  1893. (split-string
  1894. (buffer-substring
  1895. (point) (org-babel-result-end)) "[\r\n]+")
  1896. "\n")))
  1897. (or (org-babel-number-p result-string) result-string))
  1898. ((looking-at org-babel-result-regexp)
  1899. (save-excursion (forward-line 1) (org-babel-read-result))))))
  1900. (defun org-babel-read-table ()
  1901. "Read the table at `point' into emacs-lisp."
  1902. (mapcar (lambda (row)
  1903. (if (and (symbolp row) (equal row 'hline)) row
  1904. (mapcar (lambda (el) (org-babel-read el 'inhibit-lisp-eval)) row)))
  1905. (org-table-to-lisp)))
  1906. (defun org-babel-read-list ()
  1907. "Read the list at `point' into emacs-lisp."
  1908. (mapcar (lambda (el) (org-babel-read el 'inhibit-lisp-eval))
  1909. (cdr (org-list-to-lisp))))
  1910. (defvar org-link-types-re)
  1911. (defun org-babel-read-link ()
  1912. "Read the link at `point' into emacs-lisp.
  1913. If the path of the link is a file path it is expanded using
  1914. `expand-file-name'."
  1915. (let* ((case-fold-search t)
  1916. (raw (and (looking-at org-bracket-link-regexp)
  1917. (org-no-properties (match-string 1))))
  1918. (type (and (string-match org-link-types-re raw)
  1919. (match-string 1 raw))))
  1920. (cond
  1921. ((not type) (expand-file-name raw))
  1922. ((string= type "file")
  1923. (and (string-match "file\\(.*\\):\\(.+\\)" raw)
  1924. (expand-file-name (match-string 2 raw))))
  1925. (t raw))))
  1926. (defun org-babel-format-result (result &optional sep)
  1927. "Format RESULT for writing to file."
  1928. (let ((echo-res (lambda (r) (if (stringp r) r (format "%S" r)))))
  1929. (if (listp result)
  1930. ;; table result
  1931. (orgtbl-to-generic
  1932. result (list :sep (or sep "\t") :fmt echo-res))
  1933. ;; scalar result
  1934. (funcall echo-res result))))
  1935. (defun org-babel-insert-result
  1936. (result &optional result-params info hash indent lang)
  1937. "Insert RESULT into the current buffer.
  1938. By default RESULT is inserted after the end of the current source
  1939. block. The RESULT of an inline source block usually will be
  1940. wrapped inside a `results' macro and placed on the same line as
  1941. the inline source block. The macro is stripped upon export.
  1942. Multiline and non-scalar RESULTS from inline source blocks are
  1943. not allowed. With optional argument RESULT-PARAMS controls
  1944. insertion of results in the Org mode file. RESULT-PARAMS can
  1945. take the following values:
  1946. replace - (default option) insert results after the source block
  1947. or inline source block replacing any previously
  1948. inserted results.
  1949. silent -- no results are inserted into the Org-mode buffer but
  1950. the results are echoed to the minibuffer and are
  1951. ingested by Emacs (a potentially time consuming
  1952. process).
  1953. file ---- the results are interpreted as a file path, and are
  1954. inserted into the buffer using the Org-mode file syntax.
  1955. list ---- the results are interpreted as an Org-mode list.
  1956. raw ----- results are added directly to the Org-mode file. This
  1957. is a good option if you code block will output org-mode
  1958. formatted text.
  1959. drawer -- results are added directly to the Org-mode file as with
  1960. \"raw\", but are wrapped in a RESULTS drawer or results
  1961. macro, allowing them to later be replaced or removed
  1962. automatically.
  1963. org ----- results are added inside of a \"src_org{}\" or \"#+BEGIN_SRC
  1964. org\" block depending on whether the current source block is
  1965. inline or not. They are not comma-escaped when inserted,
  1966. but Org syntax here will be discarded when exporting the
  1967. file.
  1968. html ---- results are added inside of a #+BEGIN_EXPORT HTML block
  1969. or html export snippet depending on whether the current
  1970. source block is inline or not. This is a good option
  1971. if your code block will output html formatted text.
  1972. latex --- results are added inside of a #+BEGIN_EXPORT LATEX
  1973. block or latex export snippet depending on whether the
  1974. current source block is inline or not. This is a good
  1975. option if your code block will output latex formatted
  1976. text.
  1977. code ---- the results are extracted in the syntax of the source
  1978. code of the language being evaluated and are added
  1979. inside of a source block with the source-code language
  1980. set appropriately. Also, source block inlining is
  1981. preserved in this case. Note this relies on the
  1982. optional LANG argument.
  1983. list ---- the results are rendered as a list. This option not
  1984. allowed for inline src blocks.
  1985. table --- the results are rendered as a table. This option not
  1986. allowed for inline src blocks.
  1987. INFO may provide the values of these header arguments (in the
  1988. `header-arguments-alist' see the docstring for
  1989. `org-babel-get-src-block-info'):
  1990. :file --- the name of the file to which output should be written.
  1991. :wrap --- the effect is similar to `latex' in RESULT-PARAMS but
  1992. using the argument supplied to specify the export block
  1993. or snippet type."
  1994. (if (stringp result)
  1995. (progn
  1996. (setq result (org-no-properties result))
  1997. (when (member "file" result-params)
  1998. (setq result (org-babel-result-to-file
  1999. result (when (assoc :file-desc (nth 2 info))
  2000. (or (cdr (assoc :file-desc (nth 2 info)))
  2001. result))))))
  2002. (unless (listp result) (setq result (format "%S" result))))
  2003. (if (and result-params (member "silent" result-params))
  2004. (progn
  2005. (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
  2006. result)
  2007. (save-excursion
  2008. (let* ((inlinep
  2009. (save-excursion
  2010. (when (or (org-babel-get-inline-src-block-matches)
  2011. (org-babel-get-lob-one-liner-matches))
  2012. (goto-char (match-end 0))
  2013. (org-babel-remove-inline-result)
  2014. (insert " ")
  2015. (point))))
  2016. (existing-result
  2017. (unless inlinep
  2018. (org-babel-where-is-src-block-result t info hash indent)))
  2019. (bad-inline-p
  2020. (when inlinep
  2021. (or
  2022. (and (member "table" result-params) "`:results table'")
  2023. (and (listp result) "list result")
  2024. (and (org-string-match-p "\n." result) "multiline result")
  2025. (and (member "list" result-params) "`:results list'"))))
  2026. (results-switches
  2027. (cdr (assoc :results_switches (nth 2 info))))
  2028. (visible-beg (point-min-marker))
  2029. (visible-end (point-max-marker))
  2030. ;; When results exist outside of the current visible
  2031. ;; region of the buffer, be sure to widen buffer to
  2032. ;; update them.
  2033. (outside-scope-p (and existing-result
  2034. (or (> visible-beg existing-result)
  2035. (<= visible-end existing-result))))
  2036. beg end)
  2037. (when (and (stringp result) ; ensure results end in a newline
  2038. (not inlinep)
  2039. (> (length result) 0)
  2040. (not (or (string-equal (substring result -1) "\n")
  2041. (string-equal (substring result -1) "\r"))))
  2042. (setq result (concat result "\n")))
  2043. (unwind-protect
  2044. (progn
  2045. (when outside-scope-p (widen))
  2046. (if (not existing-result)
  2047. (setq beg (or inlinep (point)))
  2048. (goto-char existing-result)
  2049. (save-excursion
  2050. (re-search-forward "#" nil t)
  2051. (setq indent (- (current-column) 1)))
  2052. (forward-line 1)
  2053. (setq beg (point))
  2054. (cond
  2055. ((member "replace" result-params)
  2056. (delete-region (point) (org-babel-result-end)))
  2057. ((member "append" result-params)
  2058. (goto-char (org-babel-result-end)) (setq beg (point-marker)))
  2059. ((member "prepend" result-params)))) ; already there
  2060. (setq results-switches
  2061. (if results-switches (concat " " results-switches) ""))
  2062. (let ((wrap (lambda (start finish &optional no-escape no-newlines
  2063. inline-start inline-finish)
  2064. (when inlinep
  2065. (setq start inline-start)
  2066. (setq finish inline-finish)
  2067. (setq no-newlines t))
  2068. (goto-char end)
  2069. (insert (concat finish (unless no-newlines "\n")))
  2070. (goto-char beg)
  2071. (insert (concat start (unless no-newlines "\n")))
  2072. (unless no-escape
  2073. (org-escape-code-in-region (min (point) end) end))
  2074. (goto-char end)
  2075. (unless no-newlines (goto-char (point-at-eol)))
  2076. (setq end (point-marker))))
  2077. (tabulablep
  2078. (lambda (r)
  2079. ;; Non-nil when result R can be turned into
  2080. ;; a table.
  2081. (and (listp r)
  2082. (null (cdr (last r)))
  2083. (cl-every
  2084. (lambda (e) (or (atom e) (null (cdr (last e)))))
  2085. result)))))
  2086. ;; insert results based on type
  2087. (cond
  2088. ;; Do nothing for an empty result.
  2089. ((null result))
  2090. ;; Illegal inline result or params.
  2091. (bad-inline-p
  2092. (error "Inline error: %s cannot be used" bad-inline-p))
  2093. ;; insert a list if preferred
  2094. ((member "list" result-params)
  2095. (insert
  2096. (org-babel-trim
  2097. (org-list-to-generic
  2098. (cons 'unordered
  2099. (mapcar
  2100. (lambda (e)
  2101. (list (if (stringp e) e (format "%S" e))))
  2102. (if (listp result) result
  2103. (split-string result "\n" t))))
  2104. '(:splicep nil :istart "- " :iend "\n")))
  2105. "\n"))
  2106. ;; Try hard to print RESULT as a table. Give up if
  2107. ;; it contains an improper list.
  2108. ((funcall tabulablep result)
  2109. (goto-char beg)
  2110. (insert (concat (orgtbl-to-orgtbl
  2111. (if (cl-every
  2112. (lambda (e)
  2113. (or (eq e 'hline) (listp e)))
  2114. result)
  2115. result
  2116. (list result))
  2117. nil)
  2118. "\n"))
  2119. (goto-char beg)
  2120. (when (org-at-table-p) (org-table-align))
  2121. (goto-char (org-table-end)))
  2122. ;; Print verbatim a list that cannot be turned into
  2123. ;; a table.
  2124. ((listp result) (insert (format "%s\n" result)))
  2125. ((member "file" result-params)
  2126. (when inlinep
  2127. (goto-char inlinep)
  2128. (setq result (org-macro-escape-arguments result)))
  2129. (insert result))
  2130. ((and inlinep
  2131. (not (member "raw" result-params)))
  2132. (goto-char inlinep)
  2133. (insert (org-macro-escape-arguments
  2134. (org-babel-chomp result "\n"))))
  2135. (t (goto-char beg) (insert result)))
  2136. (setq end (point-marker))
  2137. ;; possibly wrap result
  2138. (cond
  2139. (bad-inline-p) ; Do nothing.
  2140. ((assoc :wrap (nth 2 info))
  2141. (let ((name (or (cdr (assoc :wrap (nth 2 info))) "RESULTS")))
  2142. (funcall wrap (concat "#+BEGIN_" name)
  2143. (concat "#+END_" (car (org-split-string name)))
  2144. nil nil (concat "{{{results(@@" name ":") "@@)}}}")))
  2145. ((member "html" result-params)
  2146. (funcall wrap "#+BEGIN_EXPORT html" "#+END_EXPORT" nil nil
  2147. "{{{results(@@html:" "@@)}}}"))
  2148. ((member "latex" result-params)
  2149. (funcall wrap "#+BEGIN_EXPORT latex" "#+END_EXPORT" nil nil
  2150. "{{{results(@@latex:" "@@)}}}"))
  2151. ((member "org" result-params)
  2152. (goto-char beg) (if (org-at-table-p) (org-cycle))
  2153. (funcall wrap "#+BEGIN_SRC org" "#+END_SRC" nil nil
  2154. "{{{results(src_org{" "})}}}"))
  2155. ((member "code" result-params)
  2156. (let ((lang (or lang "none")))
  2157. (funcall wrap (format "#+BEGIN_SRC %s%s" lang results-switches)
  2158. "#+END_SRC" nil nil
  2159. (format "{{{results(src_%s[%s]{" lang results-switches)
  2160. "})}}}")))
  2161. ((member "raw" result-params)
  2162. (goto-char beg) (if (org-at-table-p) (org-cycle)))
  2163. ((or (member "drawer" result-params)
  2164. ;; Stay backward compatible with <7.9.2
  2165. (member "wrap" result-params))
  2166. (goto-char beg) (if (org-at-table-p) (org-cycle))
  2167. (funcall wrap ":RESULTS:" ":END:" 'no-escape nil
  2168. "{{{results(" ")}}}"))
  2169. ((and inlinep (member "file" result-params))
  2170. (funcall wrap nil nil nil nil "{{{results(" ")}}}"))
  2171. ((and (not (funcall tabulablep result))
  2172. (not (member "file" result-params)))
  2173. (let ((org-babel-inline-result-wrap
  2174. ;; Hard code {{{results(...)}}} on top of customization.
  2175. (format "{{{results(%s)}}}" org-babel-inline-result-wrap)))
  2176. (org-babel-examplify-region beg end results-switches)
  2177. (setq end (point))))))
  2178. ;; possibly indent the results to match the #+results line
  2179. (when (and (not inlinep) (numberp indent) indent (> indent 0)
  2180. ;; in this case `table-align' does the work for us
  2181. (not (and (listp result)
  2182. (member "append" result-params))))
  2183. (indent-rigidly beg end indent))
  2184. (if (null result)
  2185. (if (member "value" result-params)
  2186. (message "Code block returned no value.")
  2187. (message "Code block produced no output."))
  2188. (message "Code block evaluation complete.")))
  2189. (when outside-scope-p (narrow-to-region visible-beg visible-end))
  2190. (set-marker visible-beg nil)
  2191. (set-marker visible-end nil))))))
  2192. (defun org-babel-remove-result (&optional info keep-keyword)
  2193. "Remove the result of the current source block."
  2194. (interactive)
  2195. (let ((location (org-babel-where-is-src-block-result nil info)))
  2196. (when location
  2197. (save-excursion
  2198. (goto-char location)
  2199. (when (looking-at (concat org-babel-result-regexp ".*$"))
  2200. (delete-region
  2201. (if keep-keyword (1+ (match-end 0)) (1- (match-beginning 0)))
  2202. (progn (forward-line 1) (org-babel-result-end))))))))
  2203. (defun org-babel-remove-inline-result ()
  2204. "Remove the result of the current inline-src-block or babel call.
  2205. The result must be wrapped in a `results' macro to be removed.
  2206. Leading whitespace is trimmed."
  2207. (interactive)
  2208. (let* ((el (org-element-context))
  2209. (post-blank (org-element-property :post-blank el)))
  2210. (when (memq (org-element-type el) '(inline-src-block inline-babel-call))
  2211. (org-with-wide-buffer
  2212. (goto-char (org-element-property :end el))
  2213. (let ((el (org-element-context)))
  2214. (when (and (eq (org-element-type el) 'macro)
  2215. (string= (org-element-property :key el) "results"))
  2216. (delete-region ; And leading whitespace.
  2217. (- (org-element-property :begin el) post-blank)
  2218. (- (org-element-property :end el)
  2219. (org-element-property :post-blank el)))))))))
  2220. (defun org-babel-remove-result-one-or-many (x)
  2221. "Remove the result of the current source block.
  2222. If called with a prefix argument, remove all result blocks
  2223. in the buffer."
  2224. (interactive "P")
  2225. (if x
  2226. (org-babel-map-src-blocks nil (org-babel-remove-result))
  2227. (org-babel-remove-result)))
  2228. (defun org-babel-result-end ()
  2229. "Return the point at the end of the current set of results."
  2230. (save-excursion
  2231. (cond
  2232. ((org-at-table-p) (progn (goto-char (org-table-end)) (point)))
  2233. ((org-at-item-p) (let* ((struct (org-list-struct))
  2234. (prvs (org-list-prevs-alist struct)))
  2235. (org-list-get-list-end (point-at-bol) struct prvs)))
  2236. ((let ((case-fold-search t)) (looking-at "^\\([ \t]*\\):results:"))
  2237. (progn (re-search-forward (concat "^" (match-string 1) ":END:"))
  2238. (forward-char 1) (point)))
  2239. (t
  2240. (let ((case-fold-search t))
  2241. (if (looking-at (concat "[ \t]*#\\+begin_\\([^ \t\n\r]+\\)"))
  2242. (progn (re-search-forward (concat "[ \t]*#\\+end_" (match-string 1))
  2243. nil t)
  2244. (forward-char 1))
  2245. (while (looking-at "[ \t]*\\(: \\|:$\\|\\[\\[\\)")
  2246. (forward-line 1))))
  2247. (point)))))
  2248. (defun org-babel-result-to-file (result &optional description)
  2249. "Convert RESULT into an `org-mode' link with optional DESCRIPTION.
  2250. If the `default-directory' is different from the containing
  2251. file's directory then expand relative links."
  2252. (when (stringp result)
  2253. (format "[[file:%s]%s]"
  2254. (if (and default-directory
  2255. buffer-file-name
  2256. (not (string= (expand-file-name default-directory)
  2257. (expand-file-name
  2258. (file-name-directory buffer-file-name)))))
  2259. (expand-file-name result default-directory)
  2260. result)
  2261. (if description (concat "[" description "]") ""))))
  2262. (defvar org-babel-capitalize-example-region-markers nil
  2263. "Make true to capitalize begin/end example markers inserted by code blocks.")
  2264. (define-obsolete-function-alias
  2265. 'org-babel-examplize-region
  2266. 'org-babel-examplify-region "25.1")
  2267. (defun org-babel-examplify-region (beg end &optional results-switches)
  2268. "Comment out region using the inline `==' or `: ' org example quote."
  2269. (interactive "*r")
  2270. (let ((chars-between (lambda (b e)
  2271. (not (string-match "^[\\s]*$"
  2272. (buffer-substring b e)))))
  2273. (maybe-cap (lambda (str) (if org-babel-capitalize-example-region-markers
  2274. (upcase str) str)))
  2275. (beg-bol (save-excursion (goto-char beg) (point-at-bol)))
  2276. (end-bol (save-excursion (goto-char end) (point-at-bol)))
  2277. (end-eol (save-excursion (goto-char end) (point-at-eol))))
  2278. (if (and (not (= end end-bol))
  2279. (or (funcall chars-between beg-bol beg)
  2280. (funcall chars-between end end-eol)))
  2281. (save-excursion
  2282. (goto-char beg)
  2283. (insert (format org-babel-inline-result-wrap
  2284. (prog1 (buffer-substring beg end)
  2285. (delete-region beg end)))))
  2286. (let ((size (count-lines beg end)))
  2287. (save-excursion
  2288. (cond ((= size 0)) ; do nothing for an empty result
  2289. ((< size org-babel-min-lines-for-block-output)
  2290. (goto-char beg)
  2291. (dotimes (n size)
  2292. (beginning-of-line 1) (insert ": ") (forward-line 1)))
  2293. (t
  2294. (goto-char beg)
  2295. (insert (if results-switches
  2296. (format "%s%s\n"
  2297. (funcall maybe-cap "#+begin_example")
  2298. results-switches)
  2299. (funcall maybe-cap "#+begin_example\n")))
  2300. (if (markerp end) (goto-char end) (forward-char (- end beg)))
  2301. (insert (funcall maybe-cap "#+end_example\n")))))))))
  2302. (defun org-babel-update-block-body (new-body)
  2303. "Update the body of the current code block to NEW-BODY."
  2304. (if (not (org-babel-where-is-src-block-head))
  2305. (error "Not in a source block")
  2306. (save-match-data
  2307. (replace-match (concat (org-babel-trim (org-remove-indentation new-body))
  2308. "\n") nil t nil 5))
  2309. (indent-rigidly (match-beginning 5) (match-end 5) 2)))
  2310. (defun org-babel-merge-params (&rest plists)
  2311. "Combine all parameter association lists in PLISTS.
  2312. Later elements of PLISTS override the values of previous elements.
  2313. This takes into account some special considerations for certain
  2314. parameters when merging lists."
  2315. (let* ((results-exclusive-groups
  2316. (mapcar (lambda (group) (mapcar #'symbol-name group))
  2317. (cdr (assoc 'results org-babel-common-header-args-w-values))))
  2318. (exports-exclusive-groups
  2319. (mapcar (lambda (group) (mapcar #'symbol-name group))
  2320. (cdr (assoc 'exports org-babel-common-header-args-w-values))))
  2321. (variable-index 0)
  2322. (e-merge (lambda (exclusive-groups &rest result-params)
  2323. ;; maintain exclusivity of mutually exclusive parameters
  2324. (let (output)
  2325. (mapc (lambda (new-params)
  2326. (mapc (lambda (new-param)
  2327. (mapc (lambda (exclusive-group)
  2328. (when (member new-param exclusive-group)
  2329. (mapcar (lambda (excluded-param)
  2330. (setq output
  2331. (delete
  2332. excluded-param
  2333. output)))
  2334. exclusive-group)))
  2335. exclusive-groups)
  2336. (setq output (org-uniquify
  2337. (cons new-param output))))
  2338. new-params))
  2339. result-params)
  2340. output)))
  2341. params results exports tangle noweb cache vars shebang comments padline
  2342. clearnames)
  2343. (mapc
  2344. (lambda (plist)
  2345. (mapc
  2346. (lambda (pair)
  2347. (case (car pair)
  2348. (:var
  2349. (let ((name (if (listp (cdr pair))
  2350. (cadr pair)
  2351. (and (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*="
  2352. (cdr pair))
  2353. (intern (match-string 1 (cdr pair)))))))
  2354. (if name
  2355. (setq vars
  2356. (append
  2357. (if (member name (mapcar #'car vars))
  2358. (progn
  2359. (push name clearnames)
  2360. (delq nil
  2361. (mapcar
  2362. (lambda (p)
  2363. (unless (equal (car p) name) p))
  2364. vars)))
  2365. vars)
  2366. (list (cons name pair))))
  2367. ;; if no name is given and we already have named variables
  2368. ;; then assign to named variables in order
  2369. (if (and vars (nth variable-index vars))
  2370. (let ((name (car (nth variable-index vars))))
  2371. (push name clearnames) ; clear out colnames
  2372. ; and rownames
  2373. ; for replace vars
  2374. (prog1 (setf (cddr (nth variable-index vars))
  2375. (concat (symbol-name name) "=" (cdr pair)))
  2376. (incf variable-index)))
  2377. (error "Variable \"%s\" must be assigned a default value"
  2378. (cdr pair))))))
  2379. (:results
  2380. (setq results (funcall e-merge results-exclusive-groups
  2381. results
  2382. (split-string
  2383. (let ((r (cdr pair)))
  2384. (if (stringp r) r (eval r)))))))
  2385. (:file
  2386. (when (cdr pair)
  2387. (setq results (funcall e-merge results-exclusive-groups
  2388. results '("file")))
  2389. (unless (or (member "both" exports)
  2390. (member "none" exports)
  2391. (member "code" exports))
  2392. (setq exports (funcall e-merge exports-exclusive-groups
  2393. exports '("results"))))
  2394. (setq params (cons pair (assq-delete-all (car pair) params)))))
  2395. (:file-ext
  2396. (when (cdr pair)
  2397. (setq results (funcall e-merge results-exclusive-groups
  2398. results '("file")))
  2399. (unless (or (member "both" exports)
  2400. (member "none" exports)
  2401. (member "code" exports))
  2402. (setq exports (funcall e-merge exports-exclusive-groups
  2403. exports '("results"))))
  2404. (setq params (cons pair (assq-delete-all (car pair) params)))))
  2405. (:exports
  2406. (setq exports (funcall e-merge exports-exclusive-groups
  2407. exports
  2408. (split-string (or (cdr pair) "")))))
  2409. (:tangle ;; take the latest -- always overwrite
  2410. (setq tangle (or (list (cdr pair)) tangle)))
  2411. (:noweb
  2412. (setq noweb (funcall e-merge
  2413. '(("yes" "no" "tangle" "no-export"
  2414. "strip-export" "eval"))
  2415. noweb
  2416. (split-string (or (cdr pair) "")))))
  2417. (:cache
  2418. (setq cache (funcall e-merge '(("yes" "no")) cache
  2419. (split-string (or (cdr pair) "")))))
  2420. (:padline
  2421. (setq padline (funcall e-merge '(("yes" "no")) padline
  2422. (split-string (or (cdr pair) "")))))
  2423. (:shebang ;; take the latest -- always overwrite
  2424. (setq shebang (or (list (cdr pair)) shebang)))
  2425. (:comments
  2426. (setq comments (funcall e-merge '(("yes" "no")) comments
  2427. (split-string (or (cdr pair) "")))))
  2428. (t ;; replace: this covers e.g. :session
  2429. (setq params (cons pair (assq-delete-all (car pair) params))))))
  2430. plist))
  2431. plists)
  2432. (setq vars (reverse vars))
  2433. (while vars (setq params (cons (cons :var (cddr (pop vars))) params)))
  2434. ;; clear out col-names and row-names for replaced variables
  2435. (mapc
  2436. (lambda (name)
  2437. (mapc
  2438. (lambda (param)
  2439. (when (assoc param params)
  2440. (setf (cdr (assoc param params))
  2441. (cl-remove-if (lambda (pair) (equal (car pair) name))
  2442. (cdr (assoc param params))))
  2443. (setf params (cl-remove-if (lambda (pair) (and (equal (car pair) param)
  2444. (null (cdr pair))))
  2445. params))))
  2446. (list :colname-names :rowname-names)))
  2447. clearnames)
  2448. (mapc
  2449. (lambda (hd)
  2450. (let ((key (intern (concat ":" (symbol-name hd))))
  2451. (val (eval hd)))
  2452. (setf params (cons (cons key (mapconcat 'identity val " ")) params))))
  2453. '(results exports tangle noweb padline cache shebang comments))
  2454. params))
  2455. (defvar org-babel-use-quick-and-dirty-noweb-expansion nil
  2456. "Set to true to use regular expressions to expand noweb references.
  2457. This results in much faster noweb reference expansion but does
  2458. not properly allow code blocks to inherit the \":noweb-ref\"
  2459. header argument from buffer or subtree wide properties.")
  2460. (defun org-babel-noweb-p (params context)
  2461. "Check if PARAMS require expansion in CONTEXT.
  2462. CONTEXT may be one of :tangle, :export or :eval."
  2463. (let* (intersect
  2464. (intersect (lambda (as bs)
  2465. (when as
  2466. (if (member (car as) bs)
  2467. (car as)
  2468. (funcall intersect (cdr as) bs))))))
  2469. (funcall intersect (case context
  2470. (:tangle '("yes" "tangle" "no-export" "strip-export"))
  2471. (:eval '("yes" "no-export" "strip-export" "eval"))
  2472. (:export '("yes")))
  2473. (split-string (or (cdr (assoc :noweb params)) "")))))
  2474. (defun org-babel-expand-noweb-references (&optional info parent-buffer)
  2475. "Expand Noweb references in the body of the current source code block.
  2476. For example the following reference would be replaced with the
  2477. body of the source-code block named `example-block'.
  2478. <<example-block>>
  2479. Note that any text preceding the <<foo>> construct on a line will
  2480. be interposed between the lines of the replacement text. So for
  2481. example if <<foo>> is placed behind a comment, then the entire
  2482. replacement text will also be commented.
  2483. This function must be called from inside of the buffer containing
  2484. the source-code block which holds BODY.
  2485. In addition the following syntax can be used to insert the
  2486. results of evaluating the source-code block named `example-block'.
  2487. <<example-block()>>
  2488. Any optional arguments can be passed to example-block by placing
  2489. the arguments inside the parenthesis following the convention
  2490. defined by `org-babel-lob'. For example
  2491. <<example-block(a=9)>>
  2492. would set the value of argument \"a\" equal to \"9\". Note that
  2493. these arguments are not evaluated in the current source-code
  2494. block but are passed literally to the \"example-block\"."
  2495. (let* ((parent-buffer (or parent-buffer (current-buffer)))
  2496. (info (or info (org-babel-get-src-block-info 'light)))
  2497. (lang (nth 0 info))
  2498. (body (nth 1 info))
  2499. (ob-nww-start org-babel-noweb-wrap-start)
  2500. (ob-nww-end org-babel-noweb-wrap-end)
  2501. (comment (string= "noweb" (cdr (assoc :comments (nth 2 info)))))
  2502. (rx-prefix (concat "\\(" org-babel-src-name-regexp "\\|"
  2503. ":noweb-ref[ \t]+" "\\)"))
  2504. (new-body "")
  2505. (nb-add (lambda (text) (setq new-body (concat new-body text))))
  2506. (c-wrap (lambda (text)
  2507. (with-temp-buffer
  2508. (funcall (intern (concat lang "-mode")))
  2509. (comment-region (point) (progn (insert text) (point)))
  2510. (org-babel-trim (buffer-string)))))
  2511. index source-name evaluate prefix)
  2512. (with-temp-buffer
  2513. (setq-local org-babel-noweb-wrap-start ob-nww-start)
  2514. (setq-local org-babel-noweb-wrap-end ob-nww-end)
  2515. (insert body) (goto-char (point-min))
  2516. (setq index (point))
  2517. (while (and (re-search-forward (org-babel-noweb-wrap) nil t))
  2518. (save-match-data (setf source-name (match-string 1)))
  2519. (save-match-data (setq evaluate (string-match "(.*)" source-name)))
  2520. (save-match-data
  2521. (setq prefix
  2522. (buffer-substring (match-beginning 0)
  2523. (save-excursion
  2524. (beginning-of-line 1) (point)))))
  2525. ;; add interval to new-body (removing noweb reference)
  2526. (goto-char (match-beginning 0))
  2527. (funcall nb-add (buffer-substring index (point)))
  2528. (goto-char (match-end 0))
  2529. (setq index (point))
  2530. (funcall
  2531. nb-add
  2532. (with-current-buffer parent-buffer
  2533. (save-restriction
  2534. (widen)
  2535. (mapconcat ;; Interpose PREFIX between every line.
  2536. #'identity
  2537. (split-string
  2538. (if evaluate
  2539. (let ((raw (org-babel-ref-resolve source-name)))
  2540. (if (stringp raw) raw (format "%S" raw)))
  2541. (or
  2542. ;; Retrieve from the library of babel.
  2543. (nth 2 (assoc (intern source-name)
  2544. org-babel-library-of-babel))
  2545. ;; Return the contents of headlines literally.
  2546. (save-excursion
  2547. (when (org-babel-ref-goto-headline-id source-name)
  2548. (org-babel-ref-headline-body)))
  2549. ;; Find the expansion of reference in this buffer.
  2550. (let ((rx (concat rx-prefix source-name "[ \t\n]"))
  2551. expansion)
  2552. (save-excursion
  2553. (goto-char (point-min))
  2554. (if org-babel-use-quick-and-dirty-noweb-expansion
  2555. (while (re-search-forward rx nil t)
  2556. (let* ((i (org-babel-get-src-block-info 'light))
  2557. (body (org-babel-expand-noweb-references i))
  2558. (sep (or (cdr (assoc :noweb-sep (nth 2 i)))
  2559. "\n"))
  2560. (full (if comment
  2561. (let ((cs (org-babel-tangle-comment-links i)))
  2562. (concat (funcall c-wrap (car cs)) "\n"
  2563. body "\n"
  2564. (funcall c-wrap (cadr cs))))
  2565. body)))
  2566. (setq expansion (cons sep (cons full expansion)))))
  2567. (org-babel-map-src-blocks nil
  2568. (let ((i (org-babel-get-src-block-info 'light)))
  2569. (when (equal (or (cdr (assoc :noweb-ref (nth 2 i)))
  2570. (nth 4 i))
  2571. source-name)
  2572. (let* ((body (org-babel-expand-noweb-references i))
  2573. (sep (or (cdr (assoc :noweb-sep (nth 2 i)))
  2574. "\n"))
  2575. (full (if comment
  2576. (let ((cs (org-babel-tangle-comment-links i)))
  2577. (concat (funcall c-wrap (car cs)) "\n"
  2578. body "\n"
  2579. (funcall c-wrap (cadr cs))))
  2580. body)))
  2581. (setq expansion
  2582. (cons sep (cons full expansion)))))))))
  2583. (and expansion
  2584. (mapconcat #'identity (nreverse (cdr expansion)) "")))
  2585. ;; Possibly raise an error if named block doesn't exist.
  2586. (if (or org-babel-noweb-error-all-langs
  2587. (member lang org-babel-noweb-error-langs))
  2588. (error "%s" (concat
  2589. (org-babel-noweb-wrap source-name)
  2590. "could not be resolved (see "
  2591. "`org-babel-noweb-error-langs')"))
  2592. "")))
  2593. "[\n\r]") (concat "\n" prefix))))))
  2594. (funcall nb-add (buffer-substring index (point-max))))
  2595. new-body))
  2596. (defun org-babel--script-escape-inner (str)
  2597. (let (in-single in-double backslash out)
  2598. (mapc
  2599. (lambda (ch)
  2600. (setq
  2601. out
  2602. (if backslash
  2603. (progn
  2604. (setq backslash nil)
  2605. (cond
  2606. ((and in-single (eq ch ?'))
  2607. ;; Escaped single quote inside single quoted string:
  2608. ;; emit just a single quote, since we've changed the
  2609. ;; outer quotes to double.
  2610. (cons ch out))
  2611. ((eq ch ?\")
  2612. ;; Escaped double quote
  2613. (if in-single
  2614. ;; This should be interpreted as backslash+quote,
  2615. ;; not an escape. Emit a three backslashes
  2616. ;; followed by a quote (because one layer of
  2617. ;; quoting will be stripped by `org-babel-read').
  2618. (append (list ch ?\\ ?\\ ?\\) out)
  2619. ;; Otherwise we are in a double-quoted string. Emit
  2620. ;; a single escaped quote
  2621. (append (list ch ?\\) out)))
  2622. ((eq ch ?\\)
  2623. ;; Escaped backslash: emit a single escaped backslash
  2624. (append (list ?\\ ?\\) out))
  2625. ;; Other: emit a quoted backslash followed by whatever
  2626. ;; the character was (because one layer of quoting will
  2627. ;; be stripped by `org-babel-read').
  2628. (t (append (list ch ?\\ ?\\) out))))
  2629. (case ch
  2630. (?\[ (if (or in-double in-single)
  2631. (cons ?\[ out)
  2632. (cons ?\( out)))
  2633. (?\] (if (or in-double in-single)
  2634. (cons ?\] out)
  2635. (cons ?\) out)))
  2636. (?\{ (if (or in-double in-single)
  2637. (cons ?\{ out)
  2638. (cons ?\( out)))
  2639. (?\} (if (or in-double in-single)
  2640. (cons ?\} out)
  2641. (cons ?\) out)))
  2642. (?, (if (or in-double in-single)
  2643. (cons ?, out) (cons ?\s out)))
  2644. (?\' (if in-double
  2645. (cons ?\' out)
  2646. (setq in-single (not in-single)) (cons ?\" out)))
  2647. (?\" (if in-single
  2648. (append (list ?\" ?\\) out)
  2649. (setq in-double (not in-double)) (cons ?\" out)))
  2650. (?\\ (unless (or in-single in-double)
  2651. (error "Can't handle backslash outside string in `org-babel-script-escape'"))
  2652. (setq backslash t)
  2653. out)
  2654. (t (cons ch out))))))
  2655. (string-to-list str))
  2656. (when (or in-single in-double)
  2657. (error "Unterminated string in `org-babel-script-escape'"))
  2658. (apply #'string (reverse out))))
  2659. (defun org-babel-script-escape (str &optional force)
  2660. "Safely convert tables into elisp lists."
  2661. (unless (stringp str)
  2662. (error "`org-babel-script-escape' expects a string"))
  2663. (let ((escaped
  2664. (cond
  2665. ((and (> (length str) 2)
  2666. (or (and (string-equal "[" (substring str 0 1))
  2667. (string-equal "]" (substring str -1)))
  2668. (and (string-equal "{" (substring str 0 1))
  2669. (string-equal "}" (substring str -1)))
  2670. (and (string-equal "(" (substring str 0 1))
  2671. (string-equal ")" (substring str -1)))))
  2672. (concat "'" (org-babel--script-escape-inner str)))
  2673. ((or force
  2674. (and (> (length str) 2)
  2675. (or (and (string-equal "'" (substring str 0 1))
  2676. (string-equal "'" (substring str -1)))
  2677. ;; We need to pass double-quoted strings
  2678. ;; through the backslash-twiddling bits, even
  2679. ;; though we don't need to change their
  2680. ;; delimiters.
  2681. (and (string-equal "\"" (substring str 0 1))
  2682. (string-equal "\"" (substring str -1))))))
  2683. (org-babel--script-escape-inner str))
  2684. (t str))))
  2685. (condition-case nil (org-babel-read escaped) (error escaped))))
  2686. (defun org-babel-read (cell &optional inhibit-lisp-eval)
  2687. "Convert the string value of CELL to a number if appropriate.
  2688. Otherwise if CELL looks like lisp (meaning it starts with a
  2689. \"(\", \"\\='\", \"\\=`\" or a \"[\") then read and evaluate it as
  2690. lisp, otherwise return it unmodified as a string. Optional
  2691. argument INHIBIT-LISP-EVAL inhibits lisp evaluation for
  2692. situations in which is it not appropriate."
  2693. (if (and (stringp cell) (not (equal cell "")))
  2694. (or (org-babel-number-p cell)
  2695. (if (and (not inhibit-lisp-eval)
  2696. (or (member (substring cell 0 1) '("(" "'" "`" "["))
  2697. (string= cell "*this*")))
  2698. (eval (read cell))
  2699. (if (string= (substring cell 0 1) "\"")
  2700. (read cell)
  2701. (progn (set-text-properties 0 (length cell) nil cell) cell))))
  2702. cell))
  2703. (defun org-babel-number-p (string)
  2704. "If STRING represents a number return its value."
  2705. (if (and (string-match "[0-9]+" string)
  2706. (string-match "^-?[0-9]*\\.?[0-9]*$" string)
  2707. (= (length (substring string (match-beginning 0)
  2708. (match-end 0)))
  2709. (length string)))
  2710. (string-to-number string)))
  2711. (defun org-babel-import-elisp-from-file (file-name &optional separator)
  2712. "Read the results located at FILE-NAME into an elisp table.
  2713. If the table is trivial, then return it as a scalar."
  2714. (let (result)
  2715. (save-window-excursion
  2716. (with-temp-buffer
  2717. (condition-case err
  2718. (progn
  2719. (org-table-import file-name separator)
  2720. (delete-file file-name)
  2721. (setq result (mapcar (lambda (row)
  2722. (mapcar #'org-babel-string-read row))
  2723. (org-table-to-lisp))))
  2724. (error (message "Error reading results: %s" err) nil)))
  2725. (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
  2726. (if (consp (car result))
  2727. (if (null (cdr (car result)))
  2728. (caar result)
  2729. result)
  2730. (car result))
  2731. result))))
  2732. (defun org-babel-string-read (cell)
  2733. "Strip nested \"s from around strings."
  2734. (org-babel-read (or (and (stringp cell)
  2735. (string-match "\\\"\\(.+\\)\\\"" cell)
  2736. (match-string 1 cell))
  2737. cell) t))
  2738. (defun org-babel-chomp (string &optional regexp)
  2739. "Strip a trailing space or carriage return from STRING.
  2740. The default regexp used is \"[ \\f\\t\\n\\r\\v]\" but another one
  2741. can be specified as the REGEXP argument."
  2742. (let ((regexp (or regexp "[ \f\t\n\r\v]")))
  2743. (while (and (> (length string) 0)
  2744. (string-match regexp (substring string -1)))
  2745. (setq string (substring string 0 -1)))
  2746. string))
  2747. (defun org-babel-trim (string &optional regexp)
  2748. "Strip a leading and trailing space or carriage return from STRING.
  2749. Like `org-babel-chomp', but run on both the first and last
  2750. character of the string."
  2751. (org-babel-chomp
  2752. (org-reverse-string
  2753. (org-babel-chomp (org-reverse-string string) regexp)) regexp))
  2754. (defun org-babel-tramp-handle-call-process-region
  2755. (start end program &optional delete buffer display &rest args)
  2756. "Use Tramp to handle `call-process-region'.
  2757. Fixes a bug in `tramp-handle-call-process-region'."
  2758. (if (and (featurep 'tramp) (file-remote-p default-directory))
  2759. (let ((tmpfile (tramp-compat-make-temp-file "")))
  2760. (write-region start end tmpfile)
  2761. (when delete (delete-region start end))
  2762. (unwind-protect
  2763. ;; (apply 'call-process program tmpfile buffer display args)
  2764. ;; bug in tramp
  2765. (apply 'process-file program tmpfile buffer display args)
  2766. (delete-file tmpfile)))
  2767. ;; org-babel-call-process-region-original is the original emacs
  2768. ;; definition. It is in scope from the let binding in
  2769. ;; org-babel-execute-src-block
  2770. (apply org-babel-call-process-region-original
  2771. start end program delete buffer display args)))
  2772. (defun org-babel-local-file-name (file)
  2773. "Return the local name component of FILE."
  2774. (or (file-remote-p file 'localname) file))
  2775. (defun org-babel-process-file-name (name &optional no-quote-p)
  2776. "Prepare NAME to be used in an external process.
  2777. If NAME specifies a remote location, the remote portion of the
  2778. name is removed, since in that case the process will be executing
  2779. remotely. The file name is then processed by `expand-file-name'.
  2780. Unless second argument NO-QUOTE-P is non-nil, the file name is
  2781. additionally processed by `shell-quote-argument'"
  2782. (let ((f (org-babel-local-file-name (expand-file-name name))))
  2783. (if no-quote-p f (shell-quote-argument f))))
  2784. (defvar org-babel-temporary-directory)
  2785. (unless (or noninteractive (boundp 'org-babel-temporary-directory))
  2786. (defvar org-babel-temporary-directory
  2787. (or (and (boundp 'org-babel-temporary-directory)
  2788. (file-exists-p org-babel-temporary-directory)
  2789. org-babel-temporary-directory)
  2790. (make-temp-file "babel-" t))
  2791. "Directory to hold temporary files created to execute code blocks.
  2792. Used by `org-babel-temp-file'. This directory will be removed on
  2793. Emacs shutdown."))
  2794. (defcustom org-babel-remote-temporary-directory "/tmp/"
  2795. "Directory to hold temporary files on remote hosts."
  2796. :group 'org-babel
  2797. :type 'string)
  2798. (defmacro org-babel-result-cond (result-params scalar-form &rest table-forms)
  2799. "Call the code to parse raw string results according to RESULT-PARAMS."
  2800. (declare (indent 1)
  2801. (debug (form form &rest form)))
  2802. (org-with-gensyms (params)
  2803. `(let ((,params ,result-params))
  2804. (unless (member "none" ,params)
  2805. (if (or (member "scalar" ,params)
  2806. (member "verbatim" ,params)
  2807. (member "html" ,params)
  2808. (member "code" ,params)
  2809. (member "pp" ,params)
  2810. (member "file" ,params)
  2811. (and (or (member "output" ,params)
  2812. (member "raw" ,params)
  2813. (member "org" ,params)
  2814. (member "drawer" ,params))
  2815. (not (member "table" ,params))))
  2816. ,scalar-form
  2817. ,@table-forms)))))
  2818. (def-edebug-spec org-babel-result-cond (form form body))
  2819. (defun org-babel-temp-file (prefix &optional suffix)
  2820. "Create a temporary file in the `org-babel-temporary-directory'.
  2821. Passes PREFIX and SUFFIX directly to `make-temp-file' with the
  2822. value of `temporary-file-directory' temporarily set to the value
  2823. of `org-babel-temporary-directory'."
  2824. (if (file-remote-p default-directory)
  2825. (let ((prefix
  2826. (concat (file-remote-p default-directory)
  2827. (expand-file-name
  2828. prefix org-babel-remote-temporary-directory))))
  2829. (make-temp-file prefix nil suffix))
  2830. (let ((temporary-file-directory
  2831. (or (and (boundp 'org-babel-temporary-directory)
  2832. (file-exists-p org-babel-temporary-directory)
  2833. org-babel-temporary-directory)
  2834. temporary-file-directory)))
  2835. (make-temp-file prefix nil suffix))))
  2836. (defun org-babel-remove-temporary-directory ()
  2837. "Remove `org-babel-temporary-directory' on Emacs shutdown."
  2838. (when (and (boundp 'org-babel-temporary-directory)
  2839. (file-exists-p org-babel-temporary-directory))
  2840. ;; taken from `delete-directory' in files.el
  2841. (condition-case nil
  2842. (progn
  2843. (mapc (lambda (file)
  2844. ;; This test is equivalent to
  2845. ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
  2846. ;; but more efficient
  2847. (if (eq t (car (file-attributes file)))
  2848. (delete-directory file)
  2849. (delete-file file)))
  2850. ;; We do not want to delete "." and "..".
  2851. (directory-files org-babel-temporary-directory 'full
  2852. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
  2853. (delete-directory org-babel-temporary-directory))
  2854. (error
  2855. (message "Failed to remove temporary Org-babel directory %s"
  2856. (if (boundp 'org-babel-temporary-directory)
  2857. org-babel-temporary-directory
  2858. "[directory not defined]"))))))
  2859. (add-hook 'kill-emacs-hook 'org-babel-remove-temporary-directory)
  2860. (defun org-babel-one-header-arg-safe-p (pair safe-list)
  2861. "Determine if the PAIR is a safe babel header arg according to SAFE-LIST.
  2862. For the format of SAFE-LIST, see `org-babel-safe-header-args'."
  2863. (and (consp pair)
  2864. (keywordp (car pair))
  2865. (stringp (cdr pair))
  2866. (or
  2867. (memq (car pair) safe-list)
  2868. (let ((entry (assq (car pair) safe-list)))
  2869. (and entry
  2870. (consp entry)
  2871. (cond ((functionp (cdr entry))
  2872. (funcall (cdr entry) (cdr pair)))
  2873. ((listp (cdr entry))
  2874. (member (cdr pair) (cdr entry)))
  2875. (t nil)))))))
  2876. (defun org-babel-generate-file-param (src-name params)
  2877. "Calculate the filename for source block results.
  2878. The directory is calculated from the :output-dir property of the
  2879. source block; if not specified, use the current directory.
  2880. If the source block has a #+NAME and the :file parameter does not
  2881. contain any period characters, then the :file parameter is
  2882. treated as an extension, and the output file name is the
  2883. concatenation of the directory (as calculated above), the block
  2884. name, a period, and the parameter value as a file extension.
  2885. Otherwise, the :file parameter is treated as a full file name,
  2886. and the output file name is the directory (as calculated above)
  2887. plus the parameter value."
  2888. (let* ((file-cons (assq :file params))
  2889. (file-ext-cons (assq :file-ext params))
  2890. (file-ext (cdr-safe file-ext-cons))
  2891. (dir (cdr-safe (assq :output-dir params)))
  2892. fname)
  2893. ;; create the output-dir if it does not exist
  2894. (when dir
  2895. (make-directory dir t))
  2896. (if file-cons
  2897. ;; :file given; add :output-dir if given
  2898. (when dir
  2899. (setcdr file-cons (concat (file-name-as-directory dir) (cdr file-cons))))
  2900. ;; :file not given; compute from name and :file-ext if possible
  2901. (when (and src-name file-ext)
  2902. (if dir
  2903. (setq fname (concat (file-name-as-directory (or dir ""))
  2904. src-name "." file-ext))
  2905. (setq fname (concat src-name "." file-ext)))
  2906. (setq params (cons (cons :file fname) params))))
  2907. params))
  2908. ;;; Used by backends: R, Maxima, Octave.
  2909. (defun org-babel-graphical-output-file (params)
  2910. "File where a babel block should send graphical output, per PARAMS."
  2911. (unless (assq :file params)
  2912. (if (assq :file-ext params)
  2913. (user-error ":file-ext given but no :file generated; did you forget to give a block a #+NAME?")
  2914. (user-error "No :file header argument given; cannot create graphical result.")))
  2915. (and (member "graphics" (cdr (assq :result-params params)))
  2916. (cdr (assq :file params))))
  2917. (provide 'ob-core)
  2918. ;; Local variables:
  2919. ;; generated-autoload-file: "org-loaddefs.el"
  2920. ;; End:
  2921. ;;; ob-core.el ends here