ob-core.el 120 KB

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