ob.el 98 KB

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