ob-core.el 120 KB

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