ob-core.el 120 KB

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