ob-core.el 120 KB

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