ob.el 98 KB

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