ob.el 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. ;;; ob.el --- working with code blocks in org-mode
  2. ;; Copyright (C) 2009 Eric Schulte, Dan Davison
  3. ;; Author: Eric Schulte, Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; See org-babel.org in the parent directory for more information
  24. ;;; Code:
  25. (eval-when-compile (require 'cl))
  26. (require 'org)
  27. (defvar org-babel-interpreters nil)
  28. (defvar org-babel-call-process-region-original)
  29. (defvar org-babel-lob-one-liner-regexp)
  30. (declare-function orgtbl-to-generic "org-table" (table params))
  31. (declare-function org-babel-ref-split-args "ob-ref" (arg-string))
  32. (declare-function org-babel-ref-variables "ob-ref" (params))
  33. (declare-function org-babel-lob-get-info "ob-lob" nil)
  34. (declare-function orgtbl-to-orgtbl "org-table" (table params))
  35. (declare-function org-babel-ref-resolve-reference
  36. "ob-ref" (ref &optional params))
  37. (declare-function tramp-compat-make-temp-file
  38. "tramp" (filename &optional dir-flag))
  39. (declare-function tramp-dissect-file-name
  40. "tramp" (name &optional nodefault))
  41. (declare-function tramp-file-name-user "tramp" (vec))
  42. (declare-function tramp-file-name-host "tramp" (vec))
  43. (defvar org-babel-source-name-regexp
  44. "^[ \t]*#\\+\\(srcname\\|source\\|function\\):[ \t]*"
  45. "Regular expression used to match a source name line.")
  46. (defvar org-babel-src-block-regexp nil
  47. "Regexp used to test when inside of a org-babel src-block")
  48. (defvar org-babel-inline-src-block-regexp nil
  49. "Regexp used to test when on an inline org-babel src-block")
  50. (defun org-babel-get-src-block-info (&optional header-vars-only)
  51. "Get information of the current source block.
  52. Returns a list
  53. (language body header-arguments-alist switches name function-args indent).
  54. Unless HEADER-VARS-ONLY is non-nil, any variable
  55. references provided in 'function call style' (i.e. in a
  56. parenthesised argument list following the src block name) are
  57. added to the header-arguments-alist."
  58. (let ((case-fold-search t) head info args indent)
  59. (if (setq head (org-babel-where-is-src-block-head))
  60. (save-excursion
  61. (goto-char head)
  62. (setq info (org-babel-parse-src-block-match))
  63. (setq indent (car (last info)))
  64. (setq info (butlast info))
  65. (forward-line -1)
  66. (if (looking-at
  67. (concat org-babel-source-name-regexp
  68. "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
  69. (progn
  70. (setq info (append info (list (org-babel-clean-text-properties
  71. (match-string 2)))))
  72. ;; Note that e.g. "name()" and "name( )" result in
  73. ;; ((:var . "")). We maintain that behaviour, and the
  74. ;; resulting non-nil sixth element is relied upon in
  75. ;; org-babel-exp-code to detect a functional-style
  76. ;; block in those cases. However, "name" without any
  77. ;; parentheses would result in the same thing, so we
  78. ;; explicitly avoid that.
  79. (if (setq args (match-string 4))
  80. (setq info
  81. (append info (list
  82. (mapcar
  83. (lambda (ref) (cons :var ref))
  84. (org-babel-ref-split-args args))))))
  85. (unless header-vars-only
  86. (setf (nth 2 info)
  87. (org-babel-merge-params (nth 5 info) (nth 2 info)))))
  88. (setq info (append info (list nil nil))))
  89. (append info (list indent)))
  90. (if (save-excursion ;; inline source block
  91. (re-search-backward "[ \f\t\n\r\v]" nil t)
  92. (looking-at org-babel-inline-src-block-regexp))
  93. (org-babel-parse-inline-src-block-match)
  94. nil))))
  95. (defun org-babel-execute-src-block-maybe ()
  96. "Detect if this is context for a org-babel src-block and if so
  97. then run `org-babel-execute-src-block'."
  98. (interactive)
  99. (let ((info (org-babel-get-src-block-info)))
  100. (if info
  101. (progn (org-babel-execute-src-block current-prefix-arg info) t) nil)))
  102. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-src-block-maybe)
  103. (defun org-babel-expand-src-block-maybe ()
  104. "Detect if this is context for a org-babel src-block and if so
  105. then run `org-babel-expand-src-block'."
  106. (interactive)
  107. (let ((info (org-babel-get-src-block-info)))
  108. (if info
  109. (progn (org-babel-expand-src-block current-prefix-arg info) t)
  110. nil)))
  111. (defadvice org-edit-special (around org-babel-prep-session-for-edit activate)
  112. "Prepare the current source block's session according to it's
  113. header arguments before editing in an org-src buffer. This
  114. function is called when `org-edit-special' is called with a
  115. prefix argument from inside of a source-code block."
  116. (when current-prefix-arg
  117. (let* ((info (org-babel-get-src-block-info))
  118. (lang (nth 0 info))
  119. (params (nth 2 info))
  120. (session (cdr (assoc :session params))))
  121. (when (and info session) ;; we are in a source-code block with a session
  122. (funcall
  123. (intern (concat "org-babel-prep-session:" lang)) session params))))
  124. ad-do-it)
  125. (defadvice org-open-at-point (around org-babel-open-at-point activate)
  126. "If `point' is on a source code block, then open that block's
  127. results with `org-babel-open-src-block-results', otherwise defer
  128. to `org-open-at-point'."
  129. (interactive "P")
  130. (or (call-interactively #'org-babel-open-src-block-result) ad-do-it))
  131. (defun org-babel-load-in-session-maybe ()
  132. "Detect if this is context for a org-babel src-block and if so
  133. then run `org-babel-load-in-session'."
  134. (interactive)
  135. (let ((info (org-babel-get-src-block-info)))
  136. (if info
  137. (progn (org-babel-load-in-session current-prefix-arg info) t)
  138. nil)))
  139. (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
  140. (defun org-babel-pop-to-session-maybe ()
  141. "Detect if this is context for a org-babel src-block and if so
  142. then run `org-babel-pop-to-session'."
  143. (interactive)
  144. (let ((info (org-babel-get-src-block-info)))
  145. (if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil)))
  146. (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
  147. (defconst org-babel-header-arg-names
  148. '(cache cmdline colnames dir exports file noweb results
  149. session tangle var noeval)
  150. "Common header arguments used by org-babel. Note that
  151. individual languages may define their own language specific
  152. header arguments as well.")
  153. (defvar org-babel-default-header-args
  154. '((:session . "none") (:results . "replace") (:exports . "code")
  155. (:cache . "no") (:noweb . "no") (:hlines . "no"))
  156. "Default arguments to use when evaluating a source block.")
  157. (defvar org-babel-default-inline-header-args
  158. '((:session . "none") (:results . "silent") (:exports . "results"))
  159. "Default arguments to use when evaluating an inline source block.")
  160. (defvar org-babel-current-buffer-properties)
  161. (make-variable-buffer-local 'org-babel-current-buffer-properties)
  162. (defvar org-babel-result-regexp
  163. "^[ \t]*#\\+res\\(ults\\|name\\)\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:"
  164. "Regular expression used to match result lines. If the
  165. results are associated with a hash key then the hash will be
  166. saved in the second match data.")
  167. (defvar org-babel-min-lines-for-block-output 10
  168. "If number of lines of output is equal to or exceeds this
  169. value, the output is placed in a #+begin_example...#+end_example
  170. block. Otherwise the output is marked as literal by inserting
  171. colons at the starts of the lines. This variable only takes
  172. effect if the :results output option is in effect.")
  173. (defvar org-babel-noweb-error-langs nil
  174. "List of language for which errors should be raised when the
  175. source code block satisfying a noweb reference in this language
  176. can not be resolved.")
  177. (defvar org-babel-hash-show 4
  178. "Number of initial characters to show of a hidden results hash.")
  179. (defvar org-babel-after-execute-hook nil
  180. "Hook for functions to be called after `org-babel-execute-src-block'")
  181. (defun org-babel-named-src-block-regexp-for-name (name)
  182. "This generates a regexp used to match a src block named NAME."
  183. (concat org-babel-source-name-regexp (regexp-quote name) "[ \t\n]*"
  184. (substring org-babel-src-block-regexp 1)))
  185. (defun org-babel-add-interpreter (interpreter)
  186. "Add INTERPRETER to `org-babel-interpreters' and update
  187. `org-babel-src-block-regexp' appropriately."
  188. (unless (member interpreter org-babel-interpreters)
  189. (setq org-babel-interpreters
  190. (sort (cons interpreter org-babel-interpreters)
  191. (lambda (left right)
  192. (> (length left) (length right)))))
  193. (org-babel-set-interpreters 'org-babel-interpreters org-babel-interpreters)))
  194. (defun org-babel-set-interpreters (var value)
  195. "Update the regular expressions used to match block and inline
  196. code."
  197. (set-default var value)
  198. (setq
  199. org-babel-src-block-regexp
  200. (concat "^\\([ \t]*\\)#\\+begin_src[ \t]+\\(" ;; (1) indentation (2) lang
  201. (mapconcat 'regexp-quote value "\\|")
  202. "\\)[ \t]*"
  203. "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)" ;; (3) switches
  204. "\\([^\n]*\\)\n" ;; (4) header arguments
  205. "\\([^\000]+?\n\\)[ \t]*#\\+end_src"));; (5) body
  206. (setq org-babel-inline-src-block-regexp
  207. (concat "[ \f\t\n\r\v]\\(src_" ;; (1) replacement target
  208. "\\(" ;; (2) lang
  209. (mapconcat 'regexp-quote value "\\|")
  210. "\\)"
  211. "\\(\\|\\[\\(.*?\\)\\]\\)" ;; (3,4) (unused, headers)
  212. "{\\([^\f\n\r\v]+?\\)}" ;; (5) body
  213. "\\)")))
  214. (defcustom org-babel-interpreters '()
  215. "Interpreters allows for evaluation tags.
  216. This is a list of program names (as strings) that can evaluate code and
  217. insert the output into an Org-mode buffer. Valid choices are
  218. R Evaluate R code
  219. emacs-lisp Evaluate Emacs Lisp code and display the result
  220. sh Pass command to the shell and display the result
  221. perl The perl interpreter
  222. python The python interpreter
  223. ruby The ruby interpreter
  224. The source block regexp `org-babel-src-block-regexp' is updated
  225. when a new interpreter is added to this list through the
  226. customize interface. To add interpreters to this variable from
  227. lisp code use the `org-babel-add-interpreter' function."
  228. :group 'org-babel
  229. :set 'org-babel-set-interpreters
  230. :type '(set :greedy t
  231. (const "R")
  232. (const "emacs-lisp")
  233. (const "sh")
  234. (const "perl")
  235. (const "python")
  236. (const "ruby")))
  237. ;;; functions
  238. (defvar call-process-region)
  239. (defun org-babel-execute-src-block (&optional arg info params)
  240. "Execute the current source code block, and insert the results
  241. into the buffer. Source code execution and the collection and
  242. formatting of results can be controlled through a variety of
  243. header arguments.
  244. Optionally supply a value for INFO in the form returned by
  245. `org-babel-get-src-block-info'.
  246. Optionally supply a value for PARAMS which will be merged with
  247. the header arguments specified at the front of the source code
  248. block."
  249. (interactive)
  250. (let* ((info (or info (org-babel-get-src-block-info)))
  251. (lang (nth 0 info))
  252. (params (setf (nth 2 info)
  253. (sort (org-babel-merge-params (nth 2 info) params)
  254. (lambda (el1 el2) (string< (symbol-name (car el1))
  255. (symbol-name (car el2)))))))
  256. (new-hash
  257. (if (and (cdr (assoc :cache params))
  258. (string= "yes" (cdr (assoc :cache params))))
  259. (org-babel-sha1-hash info)))
  260. (old-hash (org-babel-result-hash info))
  261. (body (setf (nth 1 info)
  262. (if (and (cdr (assoc :noweb params))
  263. (string= "yes" (cdr (assoc :noweb params))))
  264. (org-babel-expand-noweb-references info)
  265. (nth 1 info))))
  266. (result-params (split-string (or (cdr (assoc :results params)) "")))
  267. (result-type (cond ((member "output" result-params) 'output)
  268. ((member "value" result-params) 'value)
  269. (t 'value)))
  270. (cmd (intern (concat "org-babel-execute:" lang)))
  271. (dir (cdr (assoc :dir params)))
  272. (default-directory
  273. (or (and dir (file-name-as-directory dir)) default-directory))
  274. (org-babel-call-process-region-original
  275. (if (boundp 'org-babel-call-process-region-original) org-babel-call-process-region-original
  276. (symbol-function 'call-process-region)))
  277. (indent (car (last info)))
  278. result)
  279. (unwind-protect
  280. (flet ((call-process-region (&rest args)
  281. (apply 'org-babel-tramp-handle-call-process-region args)))
  282. (unless (member lang org-babel-interpreters)
  283. (error "Language is not in `org-babel-interpreters': %s" lang))
  284. (if (and (not arg) new-hash (equal new-hash old-hash))
  285. (save-excursion ;; return cached result
  286. (goto-char (org-babel-where-is-src-block-result nil info))
  287. (end-of-line 1) (forward-char 1)
  288. (setq result (org-babel-read-result))
  289. (message (replace-regexp-in-string "%" "%%"
  290. (format "%S" result))) result)
  291. (setq result (funcall cmd body params))
  292. (if (eq result-type 'value)
  293. (setq result (if (and (or (member "vector" result-params)
  294. (member "table" result-params))
  295. (not (listp result)))
  296. (list (list result))
  297. result)))
  298. (org-babel-insert-result
  299. result result-params info new-hash indent lang)
  300. (run-hooks 'org-babel-after-execute-hook)
  301. result))
  302. (setq call-process-region 'org-babel-call-process-region-original))))
  303. (defun org-babel-expand-body:generic (body params &optional processed-params)
  304. "Expand a block of code with org-babel according to it's header
  305. arguments. This generic implementation of body expansion is
  306. called for languages which have not defined their own specific
  307. org-babel-expand-body:lang function." body)
  308. (defun org-babel-expand-src-block (&optional arg info params)
  309. "Expand the current source code block according to it's header
  310. arguments, and pop open the results in a preview buffer."
  311. (interactive)
  312. (let* ((info (or info (org-babel-get-src-block-info)))
  313. (lang (nth 0 info))
  314. (params (setf (nth 2 info)
  315. (sort (org-babel-merge-params (nth 2 info) params)
  316. (lambda (el1 el2) (string< (symbol-name (car el1))
  317. (symbol-name (car el2)))))))
  318. (body (setf (nth 1 info)
  319. (if (and (cdr (assoc :noweb params))
  320. (string= "yes" (cdr (assoc :noweb params))))
  321. (org-babel-expand-noweb-references info) (nth 1 info))))
  322. (cmd (intern (concat "org-babel-expand-body:" lang)))
  323. (expanded (funcall (if (fboundp cmd) cmd 'org-babel-expand-body:generic)
  324. body params)))
  325. (org-edit-src-code
  326. nil expanded (concat "*Org-Babel Preview " (buffer-name) "[ " lang " ]*"))))
  327. (defun org-babel-load-in-session (&optional arg info)
  328. "Load the body of the current source-code block. Evaluate the
  329. header arguments for the source block before entering the
  330. session. After loading the body this pops open the session."
  331. (interactive)
  332. (let* ((info (or info (org-babel-get-src-block-info)))
  333. (lang (nth 0 info))
  334. (body (nth 1 info))
  335. (params (nth 2 info))
  336. (session (cdr (assoc :session params))))
  337. (unless (member lang org-babel-interpreters)
  338. (error "Language is not in `org-babel-interpreters': %s" lang))
  339. ;; if called with a prefix argument, then process header arguments
  340. (pop-to-buffer
  341. (funcall (intern (concat "org-babel-load-session:" lang))
  342. session body params))
  343. (end-of-line 1)))
  344. (defun org-babel-switch-to-session (&optional arg info)
  345. "Switch to the session of the current source-code block.
  346. If called with a prefix argument then evaluate the header arguments
  347. for the source block before entering the session. Copy the body
  348. of the source block to the kill ring."
  349. (interactive)
  350. (let* ((info (or info (org-babel-get-src-block-info)))
  351. (lang (nth 0 info))
  352. (body (nth 1 info))
  353. (params (nth 2 info))
  354. (session (cdr (assoc :session params)))
  355. (dir (cdr (assoc :dir params)))
  356. (default-directory
  357. (or (and dir (file-name-as-directory dir)) default-directory)))
  358. (unless (member lang org-babel-interpreters)
  359. (error "Language is not in `org-babel-interpreters': %s" lang))
  360. ;; copy body to the kill ring
  361. (with-temp-buffer (insert (org-babel-trim body))
  362. (copy-region-as-kill (point-min) (point-max)))
  363. ;; if called with a prefix argument, then process header arguments
  364. (when arg
  365. (funcall (intern (concat "org-babel-prep-session:" lang)) session params))
  366. ;; just to the session using pop-to-buffer
  367. (pop-to-buffer
  368. (funcall (intern (format "org-babel-%s-initiate-session" lang))
  369. session params))
  370. (end-of-line 1)))
  371. (defalias 'org-babel-pop-to-session 'org-babel-switch-to-session)
  372. (defun org-babel-open-src-block-result (&optional re-run)
  373. "If `point' is on a src block then open the results of the
  374. source code block, otherwise return nil. With optional prefix
  375. argument RE-RUN the source-code block is evaluated even if
  376. results already exist."
  377. (interactive "P")
  378. (when (org-babel-get-src-block-info)
  379. (save-excursion
  380. ;; go to the results, if there aren't any then run the block
  381. (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
  382. (progn (org-babel-execute-src-block)
  383. (org-babel-where-is-src-block-result))))
  384. (end-of-line 1)
  385. (while (looking-at "[\n\r\t\f ]") (forward-char 1))
  386. ;; open the results
  387. (if (looking-at org-bracket-link-regexp)
  388. ;; file results
  389. (org-open-at-point)
  390. (let ((results (org-babel-read-result)))
  391. (flet ((echo-res (result)
  392. (if (stringp result) result (format "%S" result))))
  393. (pop-to-buffer (get-buffer-create "org-babel-results"))
  394. (delete-region (point-min) (point-max))
  395. (if (listp results)
  396. ;; table result
  397. (insert (orgtbl-to-generic results '(:sep "\t" :fmt echo-res)))
  398. ;; scalar result
  399. (insert (echo-res results))))))
  400. t)))
  401. (defun org-babel-execute-buffer (&optional arg)
  402. "Call `org-babel-execute-src-block' on every source block in
  403. the current buffer."
  404. (interactive "P")
  405. (save-excursion
  406. (org-save-outline-visibility t
  407. (goto-char (point-min))
  408. (show-all)
  409. (while (re-search-forward org-babel-src-block-regexp nil t)
  410. (let ((pos-end (match-end 0)))
  411. (goto-char (match-beginning 0))
  412. (org-babel-execute-src-block arg)
  413. (goto-char pos-end))))))
  414. (defun org-babel-execute-subtree (&optional arg)
  415. "Call `org-babel-execute-src-block' on every source block in
  416. the current subtree."
  417. (interactive "P")
  418. (save-restriction
  419. (save-excursion
  420. (org-narrow-to-subtree)
  421. (org-babel-execute-buffer)
  422. (widen))))
  423. (defun org-babel-sha1-hash (&optional info)
  424. "Generate an sha1 hash based on the value of info."
  425. (interactive)
  426. (let* ((info (or info (org-babel-get-src-block-info)))
  427. (hash (sha1 (format "%s-%s" (mapconcat (lambda (arg) (format "%S" arg))
  428. (nth 2 info) ":")
  429. (nth 1 info)))))
  430. (when (interactive-p) (message hash))
  431. hash))
  432. (defun org-babel-result-hash (&optional info)
  433. "Return the in-buffer hash associated with the results
  434. specified in INFO."
  435. (org-babel-where-is-src-block-result nil info)
  436. (org-babel-clean-text-properties (match-string 3)))
  437. (defun org-babel-hide-hash ()
  438. "Hide the hash in the current results line. Only the initial
  439. `org-babel-hash-show' characters of the hash will remain
  440. visible."
  441. (add-to-invisibility-spec '(org-babel-hide-hash . t))
  442. (save-excursion
  443. (when (and (re-search-forward org-babel-result-regexp nil t)
  444. (match-string 3))
  445. (let* ((start (match-beginning 3))
  446. (hide-start (+ org-babel-hash-show start))
  447. (end (match-end 3))
  448. (hash (match-string 3))
  449. ov1 ov2)
  450. (setq ov1 (make-overlay start hide-start))
  451. (setq ov2 (make-overlay hide-start end))
  452. (overlay-put ov2 'invisible 'org-babel-hide-hash)
  453. (overlay-put ov1 'babel-hash hash)))))
  454. (defun org-babel-hide-all-hashes ()
  455. "Hide the hash in the current buffer. Only the initial
  456. `org-babel-hash-show' characters of each hash will remain
  457. visible. This function should be called as part of the
  458. `org-mode-hook'."
  459. (save-excursion
  460. (while (re-search-forward org-babel-result-regexp nil t)
  461. (goto-char (match-beginning 0))
  462. (org-babel-hide-hash)
  463. (goto-char (match-end 0)))))
  464. (add-hook 'org-mode-hook 'org-babel-hide-all-hashes)
  465. (defun org-babel-hash-at-point (&optional point)
  466. "Return the value of the hash at `point'. The hash is also
  467. added as the last element of the kill ring. This can be called
  468. with C-c C-c."
  469. (interactive)
  470. (let ((hash (car (delq nil (mapcar
  471. (lambda (ol) (overlay-get ol 'babel-hash))
  472. (overlays-at (or point (point))))))))
  473. (when hash (kill-new hash) (message hash))))
  474. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
  475. (defun org-babel-result-hide-spec ()
  476. "Add `org-babel-hide-result' as an invisibility spec for hiding
  477. portions of results lines."
  478. (add-to-invisibility-spec '(org-babel-hide-result . t)))
  479. (add-hook 'org-mode-hook 'org-babel-result-hide-spec)
  480. (defvar org-babel-hide-result-overlays nil
  481. "Overlays hiding results.")
  482. (defun org-babel-result-hide-all ()
  483. "Fold all results in the current buffer."
  484. (interactive)
  485. (org-babel-show-result-all)
  486. (save-excursion
  487. (while (re-search-forward org-babel-result-regexp nil t)
  488. (save-excursion (goto-char (match-beginning 0))
  489. (org-babel-hide-result-toggle-maybe)))))
  490. (defun org-babel-show-result-all ()
  491. "Unfold all results in the current buffer."
  492. (mapc 'delete-overlay org-babel-hide-result-overlays)
  493. (setq org-babel-hide-result-overlays nil))
  494. (defun org-babel-hide-result-toggle-maybe ()
  495. "Toggle visibility of result at point."
  496. (interactive)
  497. (let ((case-fold-search t))
  498. (if (save-excursion
  499. (beginning-of-line 1)
  500. (looking-at org-babel-result-regexp))
  501. (progn (org-babel-hide-result-toggle)
  502. t) ;; to signal that we took action
  503. nil))) ;; to signal that we did not
  504. (defun org-babel-hide-result-toggle (&optional force)
  505. "Toggle the visibility of the current result."
  506. (interactive)
  507. (save-excursion
  508. (beginning-of-line)
  509. (if (re-search-forward org-babel-result-regexp nil t)
  510. (let ((start (progn (beginning-of-line 2) (- (point) 1)))
  511. (end (progn (goto-char (- (org-babel-result-end) 1)) (point)))
  512. ov)
  513. (if (memq t (mapcar (lambda (overlay)
  514. (eq (overlay-get overlay 'invisible)
  515. 'org-babel-hide-result))
  516. (overlays-at start)))
  517. (if (or (not force) (eq force 'off))
  518. (mapc (lambda (ov)
  519. (when (member ov org-babel-hide-result-overlays)
  520. (setq org-babel-hide-result-overlays
  521. (delq ov org-babel-hide-result-overlays)))
  522. (when (eq (overlay-get ov 'invisible)
  523. 'org-babel-hide-result)
  524. (delete-overlay ov)))
  525. (overlays-at start)))
  526. (setq ov (make-overlay start end))
  527. (overlay-put ov 'invisible 'org-babel-hide-result)
  528. ;; make the block accessible to isearch
  529. (overlay-put
  530. ov 'isearch-open-invisible
  531. (lambda (ov)
  532. (when (member ov org-babel-hide-result-overlays)
  533. (setq org-babel-hide-result-overlays
  534. (delq ov org-babel-hide-result-overlays)))
  535. (when (eq (overlay-get ov 'invisible)
  536. 'org-babel-hide-result)
  537. (delete-overlay ov))))
  538. (push ov org-babel-hide-result-overlays)))
  539. (error "Not looking at a result line"))))
  540. ;; org-tab-after-check-for-cycling-hook
  541. (add-hook 'org-tab-first-hook 'org-babel-hide-result-toggle-maybe)
  542. ;; Remove overlays when changing major mode
  543. (add-hook 'org-mode-hook
  544. (lambda () (org-add-hook 'change-major-mode-hook
  545. 'org-babel-show-result-all 'append 'local)))
  546. (defmacro org-babel-map-source-blocks (file &rest body)
  547. "Evaluate BODY forms on each source-block in FILE."
  548. (declare (indent 1))
  549. `(let ((visited-p (get-file-buffer (expand-file-name ,file)))
  550. to-be-removed)
  551. (save-window-excursion
  552. (find-file ,file)
  553. (setq to-be-removed (current-buffer))
  554. (goto-char (point-min))
  555. (while (re-search-forward org-babel-src-block-regexp nil t)
  556. (goto-char (match-beginning 0))
  557. (save-match-data ,@body)
  558. (goto-char (match-end 0))))
  559. (unless visited-p
  560. (kill-buffer to-be-removed))))
  561. (defun org-babel-params-from-properties (&optional lang)
  562. "Return an association list of any source block params which
  563. may be specified in the properties of the current outline entry."
  564. (save-match-data
  565. (let (val sym)
  566. (delq nil
  567. (mapcar
  568. (lambda (header-arg)
  569. (and (setq val
  570. (or (condition-case nil
  571. (org-entry-get (point) header-arg t)
  572. (error nil))
  573. (cdr (assoc header-arg org-file-properties))))
  574. (cons (intern (concat ":" header-arg)) val)))
  575. (mapcar
  576. 'symbol-name
  577. (append
  578. org-babel-header-arg-names
  579. (progn
  580. (setq sym (intern (concat "org-babel-header-arg-names:" lang)))
  581. (and (boundp sym) (eval sym))))))))))
  582. (defun org-babel-params-from-buffer ()
  583. "Return an association list of any source block params which
  584. may be specified at the top of the current buffer."
  585. (or org-babel-current-buffer-properties
  586. (setq org-babel-current-buffer-properties
  587. (save-excursion
  588. (save-restriction
  589. (widen)
  590. (goto-char (point-min))
  591. (when (re-search-forward
  592. (org-make-options-regexp (list "BABEL")) nil t)
  593. (org-babel-parse-header-arguments
  594. (org-match-string-no-properties 2))))))))
  595. (defun org-babel-parse-src-block-match ()
  596. "Parse the match data resulting from a match of the
  597. `org-babel-src-block-regexp'."
  598. (let* ((block-indentation (length (match-string 1)))
  599. (lang (org-babel-clean-text-properties (match-string 2)))
  600. (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
  601. (switches (match-string 3))
  602. (body (org-babel-clean-text-properties (match-string 5)))
  603. (preserve-indentation (or org-src-preserve-indentation
  604. (string-match "-i\\>" switches))))
  605. (list lang
  606. ;; get block body less properties, protective commas, and indentation
  607. (with-temp-buffer
  608. (save-match-data
  609. (insert (org-babel-strip-protective-commas body))
  610. (unless preserve-indentation (org-do-remove-indentation))
  611. (buffer-string)))
  612. (org-babel-merge-params
  613. org-babel-default-header-args
  614. (org-babel-params-from-buffer)
  615. (org-babel-params-from-properties lang)
  616. (if (boundp lang-headers) (eval lang-headers) nil)
  617. (org-babel-parse-header-arguments
  618. (org-babel-clean-text-properties (or (match-string 4) ""))))
  619. switches
  620. block-indentation)))
  621. (defun org-babel-parse-inline-src-block-match ()
  622. "Parse the match data resulting from a match of the
  623. `org-babel-inline-src-block-regexp'."
  624. (let* ((lang (org-babel-clean-text-properties (match-string 2)))
  625. (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
  626. (list lang
  627. (org-babel-strip-protective-commas
  628. (org-babel-clean-text-properties (match-string 5)))
  629. (org-babel-merge-params
  630. org-babel-default-inline-header-args
  631. (org-babel-params-from-buffer)
  632. (org-babel-params-from-properties lang)
  633. (if (boundp lang-headers) (eval lang-headers) nil)
  634. (org-babel-parse-header-arguments
  635. (org-babel-clean-text-properties (or (match-string 4) "")))))))
  636. (defun org-babel-parse-header-arguments (arg-string)
  637. "Parse a string of header arguments returning an alist."
  638. (if (> (length arg-string) 0)
  639. (delq nil
  640. (mapcar
  641. (lambda (arg)
  642. (if (string-match
  643. "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)"
  644. arg)
  645. (cons (intern (concat ":" (match-string 1 arg)))
  646. (let ((raw (org-babel-chomp (match-string 2 arg))))
  647. (if (org-babel-number-p raw)
  648. raw (org-babel-read raw))))
  649. (cons (intern (concat ":" arg)) nil)))
  650. (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t)))))
  651. (defun org-babel-process-params (params)
  652. "Parse params and resolve references.
  653. Return a list (session vars result-params result-type colnames rownames)."
  654. (let* ((session (cdr (assoc :session params)))
  655. (vars-and-names (org-babel-disassemble-tables
  656. (org-babel-ref-variables params)
  657. (cdr (assoc :hlines params))
  658. (cdr (assoc :colnames params))
  659. (cdr (assoc :rownames params))))
  660. (vars (car vars-and-names))
  661. (colnames (cadr vars-and-names))
  662. (rownames (caddr vars-and-names))
  663. (result-params (split-string (or (cdr (assoc :results params)) "")))
  664. (result-type (cond ((member "output" result-params) 'output)
  665. ((member "value" result-params) 'value)
  666. (t 'value))))
  667. (list session vars result-params result-type colnames rownames)))
  668. ;; row and column names
  669. (defun org-babel-del-hlines (table)
  670. "Remove all 'hlines from TABLE."
  671. (remove 'hline table))
  672. (defun org-babel-get-colnames (table)
  673. "Return a cons cell, the `car' of which contains the TABLE less
  674. colnames, and the `cdr' of which contains a list of the column
  675. names."
  676. (if (equal 'hline (nth 1 table))
  677. (cons (cddr table) (car table))
  678. (cons (cdr table) (car table))))
  679. (defun org-babel-get-rownames (table)
  680. "Return a cons cell, the `car' of which contains the TABLE less
  681. colnames, and the `cdr' of which contains a list of the column
  682. names. Note: this function removes any hlines in TABLE."
  683. (flet ((trans (table) (apply #'mapcar* #'list table)))
  684. (let* ((width (apply 'max (mapcar (lambda (el) (if (listp el) (length el) 0)) table)))
  685. (table (trans (mapcar (lambda (row)
  686. (if (not (equal row 'hline))
  687. row
  688. (setq row '())
  689. (dotimes (n width) (setq row (cons 'hline row)))
  690. row))
  691. table))))
  692. (cons (mapcar (lambda (row) (if (equal (car row) 'hline) 'hline row))
  693. (trans (cdr table)))
  694. (remove 'hline (car table))))))
  695. (defun org-babel-put-colnames (table colnames)
  696. "Add COLNAMES to TABLE if they exist."
  697. (if colnames (apply 'list colnames 'hline table) table))
  698. (defun org-babel-put-rownames (table rownames)
  699. "Add ROWNAMES to TABLE if they exist."
  700. (if rownames
  701. (mapcar (lambda (row)
  702. (if (listp row)
  703. (cons (or (pop rownames) "") row)
  704. row)) table)
  705. table))
  706. (defun org-babel-pick-name (names selector)
  707. "Select one out of an alist of row or column names."
  708. (when names
  709. (if (and selector (symbolp selector) (not (equal t selector)))
  710. (cdr (assoc selector names))
  711. (if (integerp selector)
  712. (nth (- selector 1) names)
  713. (cdr (car (last names)))))))
  714. (defun org-babel-disassemble-tables (vars hlines colnames rownames)
  715. "Process the variables in VARS according to the HLINES,
  716. ROWNAMES and COLNAMES header arguments. Return a list consisting
  717. of the vars, cnames and rnames."
  718. (let (cnames rnames)
  719. (list
  720. (mapcar
  721. (lambda (var)
  722. (when (listp (cdr var))
  723. (when (and (not (equal colnames "no"))
  724. (or colnames (and (equal (nth 1 (cdr var)) 'hline)
  725. (not (member 'hline (cddr (cdr var)))))))
  726. (let ((both (org-babel-get-colnames (cdr var))))
  727. (setq cnames (cons (cons (car var) (cdr both))
  728. cnames))
  729. (setq var (cons (car var) (car both)))))
  730. (when (and rownames (not (equal rownames "no")))
  731. (let ((both (org-babel-get-rownames (cdr var))))
  732. (setq rnames (cons (cons (car var) (cdr both))
  733. rnames))
  734. (setq var (cons (car var) (car both)))))
  735. (when (and hlines (not (equal hlines "yes")))
  736. (setq var (cons (car var) (org-babel-del-hlines (cdr var))))))
  737. var)
  738. vars)
  739. cnames rnames)))
  740. (defun org-babel-reassemble-table (table colnames rownames)
  741. "Given a TABLE and set of COLNAMES and ROWNAMES add the names
  742. to the table for reinsertion to org-mode."
  743. (if (listp table)
  744. ((lambda (table)
  745. (if (and colnames (listp (car table)) (= (length (car table))
  746. (length colnames)))
  747. (org-babel-put-colnames table colnames) table))
  748. (if (and rownames (= (length table) (length rownames)))
  749. (org-babel-put-rownames table rownames) table))
  750. table))
  751. (defun org-babel-where-is-src-block-head ()
  752. "Return the point at the beginning of the current source
  753. block. Specifically at the beginning of the #+BEGIN_SRC line.
  754. If the point is not on a source block then return nil."
  755. (let ((initial (point)) top bottom)
  756. (or
  757. (save-excursion ;; on a source name line
  758. (beginning-of-line 1)
  759. (and (looking-at org-babel-source-name-regexp) (forward-line 1)
  760. (looking-at org-babel-src-block-regexp)
  761. (point)))
  762. (save-excursion ;; on a #+begin_src line
  763. (beginning-of-line 1)
  764. (and (looking-at org-babel-src-block-regexp)
  765. (point)))
  766. (save-excursion ;; inside a src block
  767. (and
  768. (re-search-backward "^[ \t]*#\\+begin_src" nil t) (setq top (point))
  769. (re-search-forward "^[ \t]*#\\+end_src" nil t) (setq bottom (point))
  770. (< top initial) (< initial bottom)
  771. (progn (goto-char top) (beginning-of-line 1)
  772. (looking-at org-babel-src-block-regexp))
  773. (point))))))
  774. (defun org-babel-goto-named-source-block (&optional name)
  775. "Go to a named source-code block."
  776. (interactive "ssource-block name: ")
  777. (let ((point (org-babel-find-named-block name)))
  778. (if point
  779. ;; taken from `org-open-at-point'
  780. (progn (goto-char point) (org-show-context))
  781. (message "source-code block '%s' not found in this buffer" name))))
  782. (defun org-babel-find-named-block (name)
  783. "Find a named source-code block.
  784. Return the location of the source block identified by source
  785. NAME, or nil if no such block exists. Set match data according to
  786. org-babel-named-src-block-regexp."
  787. (save-excursion
  788. (let ((case-fold-search t)
  789. (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
  790. (goto-char (point-min))
  791. (when (or (re-search-forward regexp nil t)
  792. (re-search-backward regexp nil t))
  793. (match-beginning 0)))))
  794. (defun org-babel-find-named-result (name)
  795. "Return the location of the result named NAME in the current
  796. buffer or nil if no such result exists."
  797. (save-excursion
  798. (goto-char (point-min))
  799. (when (re-search-forward
  800. (concat org-babel-result-regexp
  801. "[ \t]" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
  802. (beginning-of-line 0) (point))))
  803. (defun org-babel-where-is-src-block-result (&optional insert info hash indent)
  804. "Return the point at the beginning of the result of the current
  805. source block. Specifically at the beginning of the results line.
  806. If no result exists for this block then create a results line
  807. following the source block."
  808. (save-excursion
  809. (let* ((on-lob-line (progn (beginning-of-line 1)
  810. (looking-at org-babel-lob-one-liner-regexp)))
  811. (name (if on-lob-line
  812. (nth 0 (org-babel-lob-get-info))
  813. (nth 4 (or info (org-babel-get-src-block-info)))))
  814. (head (unless on-lob-line (org-babel-where-is-src-block-head))) end)
  815. (when head (goto-char head))
  816. (or (and name (org-babel-find-named-result name))
  817. (and (or on-lob-line (re-search-forward "^[ \t]*#\\+end_src" nil t))
  818. (progn (end-of-line 1)
  819. (if (eobp) (insert "\n") (forward-char 1))
  820. (setq end (point))
  821. (or (and (not name)
  822. (progn ;; unnamed results line already exists
  823. (re-search-forward "[^ \f\t\n\r\v]" nil t)
  824. (beginning-of-line 1)
  825. (looking-at
  826. (concat org-babel-result-regexp "\n"))))
  827. ;; or (with optional insert) back up and
  828. ;; make one ourselves
  829. (when insert
  830. (goto-char end)
  831. (if (looking-at "[\n\r]")
  832. (forward-char 1) (insert "\n"))
  833. (insert (concat
  834. (if indent
  835. (mapconcat
  836. (lambda (el) " ")
  837. (number-sequence 1 indent) "")
  838. "")
  839. "#+results"
  840. (when hash (concat "["hash"]"))
  841. ":"
  842. (when name (concat " " name)) "\n\n"))
  843. (backward-char)
  844. (beginning-of-line 0)
  845. (if hash (org-babel-hide-hash)) t)))
  846. (point))))))
  847. (defun org-babel-read-result ()
  848. "Read the result at `point' into emacs-lisp."
  849. (let ((case-fold-search t) result-string)
  850. (cond
  851. ((org-at-table-p) (org-babel-read-table))
  852. ((looking-at org-bracket-link-regexp) (org-babel-read-link))
  853. ((looking-at org-block-regexp) (org-babel-trim (match-string 4)))
  854. ((looking-at "^[ \t]*: ")
  855. (setq result-string
  856. (org-babel-trim
  857. (mapconcat (lambda (line)
  858. (if (and (> (length line) 1)
  859. (string-match "^[ \t]*: \\(.+\\)" line))
  860. (match-string 1 line)
  861. line))
  862. (split-string
  863. (buffer-substring
  864. (point) (org-babel-result-end)) "[\r\n]+")
  865. "\n")))
  866. (or (org-babel-number-p result-string) result-string))
  867. ((looking-at org-babel-result-regexp)
  868. (save-excursion (forward-line 1) (org-babel-read-result))))))
  869. (defun org-babel-read-table ()
  870. "Read the table at `point' into emacs-lisp."
  871. (mapcar (lambda (row)
  872. (if (and (symbolp row) (equal row 'hline)) row
  873. (mapcar #'org-babel-read row)))
  874. (org-table-to-lisp)))
  875. (defun org-babel-read-link ()
  876. "Read the link at `point' into emacs-lisp. If the path of the
  877. link is a file path it is expanded using `expand-file-name'."
  878. (let* ((case-fold-search t)
  879. (raw (and (looking-at org-bracket-link-regexp)
  880. (org-babel-clean-text-properties (match-string 1))))
  881. (type (and (string-match org-link-types-re raw)
  882. (match-string 1 raw))))
  883. (cond
  884. ((not type) (expand-file-name raw))
  885. ((string= type "file")
  886. (and (string-match "file\\(.*\\):\\(.+\\)" raw)
  887. (expand-file-name (match-string 2 raw))))
  888. (t raw))))
  889. (defun org-babel-insert-result
  890. (result &optional result-params info hash indent lang)
  891. "Insert RESULT into the current buffer after the end of the
  892. current source block. With optional argument RESULT-PARAMS
  893. controls insertion of results in the org-mode file.
  894. RESULT-PARAMS can take the following values...
  895. replace - (default option) insert results after the source block
  896. replacing any previously inserted results
  897. silent -- no results are inserted
  898. file ---- the results are interpreted as a file path, and are
  899. inserted into the buffer using the Org-mode file syntax
  900. raw ----- results are added directly to the org-mode file. This
  901. is a good option if you code block will output org-mode
  902. formatted text.
  903. org ----- this is the same as the 'raw' option
  904. html ---- results are added inside of a #+BEGIN_HTML block. This
  905. is a good option if you code block will output html
  906. formatted text.
  907. latex --- results are added inside of a #+BEGIN_LATEX block.
  908. This is a good option if you code block will output
  909. latex formatted text.
  910. code ---- the results are extracted in the syntax of the source
  911. code of the language being evaluated and are added
  912. inside of a #+BEGIN_SRC block with the source-code
  913. language set appropriately. Note this relies on the
  914. optional LANG argument."
  915. (if (stringp result)
  916. (progn
  917. (setq result (org-babel-clean-text-properties result))
  918. (when (member "file" result-params)
  919. (setq result (org-babel-result-to-file result))))
  920. (unless (listp result) (setq result (format "%S" result))))
  921. (if (= (length result) 0)
  922. (if (member "value" result-params)
  923. (message "No result returned by source block")
  924. (message "Source block produced no output"))
  925. (if (and result-params (member "silent" result-params))
  926. (progn
  927. (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
  928. result)
  929. (when (and (stringp result) ;; ensure results end in a newline
  930. (not (or (string-equal (substring result -1) "\n")
  931. (string-equal (substring result -1) "\r"))))
  932. (setq result (concat result "\n")))
  933. (save-excursion
  934. (let ((existing-result (org-babel-where-is-src-block-result
  935. t info hash indent))
  936. (results-switches
  937. (cdr (assoc :results_switches (nth 2 info))))
  938. beg end)
  939. (when existing-result
  940. (goto-char existing-result)
  941. (save-excursion
  942. (re-search-forward "#" nil t)
  943. (setq indent (- (current-column) 1)))
  944. (forward-line 1)
  945. (setq beg (point))
  946. (cond
  947. ((member "replace" result-params)
  948. (delete-region (point) (org-babel-result-end)))
  949. ((member "append" result-params)
  950. (goto-char (org-babel-result-end)) (setq beg (point)))
  951. ((member "prepend" result-params) ;; already there
  952. )))
  953. (setq results-switches
  954. (if results-switches (concat " " results-switches) ""))
  955. (cond
  956. ;; assume the result is a table if it's not a string
  957. ((not (stringp result))
  958. (insert (concat (orgtbl-to-orgtbl
  959. (if (or (eq 'hline (car result))
  960. (and (listp (car result))
  961. (listp (cdr (car result)))))
  962. result (list result))
  963. '(:fmt (lambda (cell) (format "%s" cell)))) "\n"))
  964. (goto-char beg) (org-table-align))
  965. ((member "file" result-params)
  966. (insert result))
  967. ((member "html" result-params)
  968. (insert (format "#+BEGIN_HTML%s\n%s#+END_HTML\n"
  969. results-switches result)))
  970. ((member "latex" result-params)
  971. (insert (format "#+BEGIN_LaTeX%s\n%s#+END_LaTeX\n"
  972. results-switches result)))
  973. ((member "code" result-params)
  974. (insert (format "#+BEGIN_SRC %s%s\n%s#+END_SRC\n"
  975. (or lang "none") results-switches result)))
  976. ((or (member "raw" result-params) (member "org" result-params))
  977. (save-excursion (insert result)) (if (org-at-table-p) (org-cycle)))
  978. (t
  979. (org-babel-examplize-region
  980. (point) (progn (insert result) (point)) results-switches)))
  981. ;; possibly indent the results to match the #+results line
  982. (setq end (if (listp result) (org-table-end) (point)))
  983. (when (and indent (> indent 0)
  984. ;; in this case `table-align' does the work for us
  985. (not (and (listp result)
  986. (member "append" result-params))))
  987. (indent-rigidly beg end indent))))
  988. (message "finished"))))
  989. (defun org-babel-remove-result (&optional info)
  990. "Remove the result of the current source block."
  991. (interactive)
  992. (let ((location (org-babel-where-is-src-block-result nil info)) start)
  993. (when location
  994. (save-excursion
  995. (goto-char location) (setq start (point)) (forward-line 1)
  996. (delete-region start (org-babel-result-end))))))
  997. (defun org-babel-result-end ()
  998. "Return the point at the end of the current set of results"
  999. (save-excursion
  1000. (if (org-at-table-p)
  1001. (progn (goto-char (org-table-end)) (point))
  1002. (let ((case-fold-search t))
  1003. (cond
  1004. ((looking-at "[ \t]*#\\+begin_latex")
  1005. (re-search-forward "[ \t]*#\\+end_latex" nil t)
  1006. (forward-line 1))
  1007. ((looking-at "[ \t]*#\\+begin_html")
  1008. (re-search-forward "[ \t]*#\\+end_html" nil t)
  1009. (forward-line 1))
  1010. ((looking-at "[ \t]*#\\+begin_example")
  1011. (re-search-forward "[ \t]*#\\+end_example" nil t)
  1012. (forward-line 1))
  1013. ((looking-at "[ \t]*#\\+begin_src")
  1014. (re-search-forward "[ \t]*#\\+end_src" nil t)
  1015. (forward-line 1))
  1016. (t (progn (while (looking-at "[ \t]*\\(: \\|\\[\\[\\)")
  1017. (forward-line 1))))))
  1018. (point))))
  1019. (defun org-babel-result-to-file (result)
  1020. "Convert RESULT into an `org-mode' link. If the
  1021. `default-directory' is different from the containing file's
  1022. directory then expand relative links."
  1023. (format
  1024. "[[file:%s]]"
  1025. (if (and default-directory
  1026. buffer-file-name
  1027. (not (string= (expand-file-name default-directory)
  1028. (expand-file-name
  1029. (file-name-directory buffer-file-name)))))
  1030. (expand-file-name result default-directory)
  1031. result)))
  1032. (defun org-babel-examplize-region (beg end &optional results-switches)
  1033. "Comment out region using the ': ' org example quote."
  1034. (interactive "*r")
  1035. (let ((size (count-lines beg end)))
  1036. (save-excursion
  1037. (cond ((= size 0)
  1038. (error (concat "This should be impossible:"
  1039. "a newline was appended to result if missing")))
  1040. ((< size org-babel-min-lines-for-block-output)
  1041. (goto-char beg)
  1042. (dotimes (n size)
  1043. (beginning-of-line 1) (insert ": ") (forward-line 1)))
  1044. (t
  1045. (goto-char beg)
  1046. (insert (if results-switches
  1047. (format "#+begin_example%s\n" results-switches)
  1048. "#+begin_example\n"))
  1049. (forward-char (- end beg))
  1050. (insert "#+end_example\n"))))))
  1051. (defun org-babel-merge-params (&rest plists)
  1052. "Combine all parameter association lists in PLISTS. Later
  1053. elements of PLISTS override the values of previous element. This
  1054. takes into account some special considerations for certain
  1055. parameters when merging lists."
  1056. (let ((results-exclusive-groups
  1057. '(("file" "vector" "table" "scalar" "raw" "org"
  1058. "html" "latex" "code" "pp")
  1059. ("replace" "silent" "append" "prepend")
  1060. ("output" "value")))
  1061. (exports-exclusive-groups
  1062. '(("code" "results" "both" "none")))
  1063. params results exports tangle noweb cache vars var ref shebang comments)
  1064. (flet ((e-merge (exclusive-groups &rest result-params)
  1065. ;; maintain exclusivity of mutually exclusive parameters
  1066. (let (output)
  1067. (mapc (lambda (new-params)
  1068. (mapc (lambda (new-param)
  1069. (mapc (lambda (exclusive-group)
  1070. (when (member new-param exclusive-group)
  1071. (mapcar (lambda (excluded-param)
  1072. (setq output
  1073. (delete
  1074. excluded-param
  1075. output)))
  1076. exclusive-group)))
  1077. exclusive-groups)
  1078. (setq output (org-uniquify
  1079. (cons new-param output))))
  1080. new-params))
  1081. result-params)
  1082. output)))
  1083. (mapc (lambda (plist)
  1084. (mapc (lambda (pair)
  1085. (case (car pair)
  1086. (:var
  1087. ;; we want only one specification per variable
  1088. (when (string-match
  1089. (concat "^\\([^= \f\t\n\r\v]+\\)[ \t]*="
  1090. "[ \t]*\\([^\f\n\r\v]+\\)$") (cdr pair))
  1091. ;; TODO: When is this not true?
  1092. (setq var (intern (match-string 1 (cdr pair)))
  1093. ref (match-string 2 (cdr pair))
  1094. vars (cons (cons var ref)
  1095. (assq-delete-all var vars)))))
  1096. (:results
  1097. (setq results
  1098. (e-merge results-exclusive-groups
  1099. results (split-string (cdr pair)))))
  1100. (:file
  1101. (when (cdr pair)
  1102. (setq results (e-merge results-exclusive-groups
  1103. results '("file")))
  1104. (unless (or (member "both" exports)
  1105. (member "none" exports)
  1106. (member "code" exports))
  1107. (setq exports (e-merge exports-exclusive-groups
  1108. exports '("results"))))
  1109. (setq params
  1110. (cons pair
  1111. (assq-delete-all (car pair) params)))))
  1112. (:exports
  1113. (setq exports
  1114. (e-merge exports-exclusive-groups
  1115. exports (split-string (cdr pair)))))
  1116. (:tangle ;; take the latest -- always overwrite
  1117. (setq tangle (or (list (cdr pair)) tangle)))
  1118. (:noweb
  1119. (setq noweb
  1120. (e-merge '(("yes" "no")) noweb
  1121. (split-string (or (cdr pair) "")))))
  1122. (:cache
  1123. (setq cache
  1124. (e-merge '(("yes" "no")) cache
  1125. (split-string (or (cdr pair) "")))))
  1126. (:shebang ;; take the latest -- always overwrite
  1127. (setq shebang (or (list (cdr pair)) shebang)))
  1128. (:comments
  1129. (setq comments
  1130. (e-merge '(("yes" "no")) comments
  1131. (split-string (or (cdr pair) "")))))
  1132. (t ;; replace: this covers e.g. :session
  1133. (setq params
  1134. (cons pair
  1135. (assq-delete-all (car pair) params))))))
  1136. plist))
  1137. plists))
  1138. (setq vars (mapcar (lambda (pair) (format "%s=%s" (car pair) (cdr pair))) vars))
  1139. (while vars (setq params (cons (cons :var (pop vars)) params)))
  1140. (cons (cons :comments (mapconcat 'identity comments " "))
  1141. (cons (cons :shebang (mapconcat 'identity shebang " "))
  1142. (cons (cons :cache (mapconcat 'identity cache " "))
  1143. (cons (cons :noweb (mapconcat 'identity noweb " "))
  1144. (cons (cons :tangle (mapconcat 'identity tangle " "))
  1145. (cons (cons :exports
  1146. (mapconcat 'identity exports " "))
  1147. (cons
  1148. (cons :results
  1149. (mapconcat 'identity results " "))
  1150. params)))))))))
  1151. (defun org-babel-expand-noweb-references (&optional info parent-buffer)
  1152. "This function expands Noweb style references in the body of
  1153. the current source-code block. For example the following
  1154. reference would be replaced with the body of the source-code
  1155. block named 'example-block'.
  1156. <<example-block>>
  1157. Note that any text preceding the <<foo>> construct on a line will
  1158. be interposed between the lines of the replacement text. So for
  1159. example if <<foo>> is placed behind a comment, then the entire
  1160. replacement text will also be commented.
  1161. This function must be called from inside of the buffer containing
  1162. the source-code block which holds BODY.
  1163. In addition the following syntax can be used to insert the
  1164. results of evaluating the source-code block named 'example-block'.
  1165. <<example-block()>>
  1166. Any optional arguments can be passed to example-block by placing
  1167. the arguments inside the parenthesis following the convention
  1168. defined by `org-babel-lob'. For example
  1169. <<example-block(a=9)>>
  1170. would set the value of argument \"a\" equal to \"9\". Note that
  1171. these arguments are not evaluated in the current source-code
  1172. block but are passed literally to the \"example-block\"."
  1173. (let* ((parent-buffer (or parent-buffer (current-buffer)))
  1174. (info (or info (org-babel-get-src-block-info)))
  1175. (lang (nth 0 info))
  1176. (body (nth 1 info))
  1177. (new-body "") index source-name evaluate prefix)
  1178. (flet ((nb-add (text)
  1179. (setq new-body (concat new-body text))))
  1180. (with-temp-buffer
  1181. (insert body) (goto-char (point-min))
  1182. (setq index (point))
  1183. (while (and (re-search-forward "<<\\(.+?\\)>>" nil t))
  1184. (save-match-data (setf source-name (match-string 1)))
  1185. (save-match-data (setq evaluate (string-match "\(.*\)" source-name)))
  1186. (save-match-data
  1187. (setq prefix
  1188. (buffer-substring (match-beginning 0)
  1189. (save-excursion
  1190. (beginning-of-line 1) (point)))))
  1191. ;; add interval to new-body (removing noweb reference)
  1192. (goto-char (match-beginning 0))
  1193. (nb-add (buffer-substring index (point)))
  1194. (goto-char (match-end 0))
  1195. (setq index (point))
  1196. (nb-add (save-current-buffer
  1197. (set-buffer parent-buffer)
  1198. (mapconcat ;; interpose `prefix' between every line
  1199. #'identity
  1200. (split-string
  1201. (if evaluate
  1202. (let ((raw (org-babel-ref-resolve-reference
  1203. source-name nil)))
  1204. (if (stringp raw) raw (format "%S" raw)))
  1205. (save-restriction
  1206. (widen)
  1207. (let ((point (org-babel-find-named-block source-name)))
  1208. (if point
  1209. (save-excursion
  1210. (goto-char point)
  1211. (org-babel-trim
  1212. (org-babel-expand-noweb-references
  1213. (org-babel-get-src-block-info))))
  1214. ;; optionally raise an error if named
  1215. ;; source-block doesn't exist
  1216. (if (member lang org-babel-noweb-error-langs)
  1217. (error
  1218. (concat "<<%s>> could not be resolved "
  1219. "(see `org-babel-noweb-error-langs')")
  1220. source-name)
  1221. "")))))
  1222. "[\n\r]") (concat "\n" prefix)))))
  1223. (nb-add (buffer-substring index (point-max)))))
  1224. new-body))
  1225. (defun org-babel-error-notify (exit-code stderr)
  1226. "Open a buffer containing information from STDERR with a
  1227. message about the value of EXIT-CODE."
  1228. (message (format "Shell command exited with code %d" exit-code))
  1229. (let ((buf (get-buffer-create "*Org-Babel Error Output*")))
  1230. (with-current-buffer buf
  1231. (goto-char (point-max))
  1232. (save-excursion (insert stderr)))
  1233. (display-buffer buf)))
  1234. (defun org-babel-clean-text-properties (text)
  1235. "Strip all properties from text return."
  1236. (when text
  1237. (set-text-properties 0 (length text) nil text) text))
  1238. (defun org-babel-strip-protective-commas (body)
  1239. "Strip protective commas from bodies of source blocks."
  1240. (replace-regexp-in-string "^,#" "#" body))
  1241. (defun org-babel-read (cell)
  1242. "Convert the string value of CELL to a number if appropriate.
  1243. Otherwise if cell looks like lisp (meaning it starts with a
  1244. \"(\" or a \"'\") then read it as lisp, otherwise return it
  1245. unmodified as a string.
  1246. This is taken almost directly from `org-read-prop'."
  1247. (if (and (stringp cell) (not (equal cell "")))
  1248. (or (org-babel-number-p cell)
  1249. (if (or (equal "(" (substring cell 0 1))
  1250. (equal "'" (substring cell 0 1))
  1251. (equal "`" (substring cell 0 1)))
  1252. (eval (read cell))
  1253. (progn (set-text-properties 0 (length cell) nil cell) cell)))
  1254. cell))
  1255. (defun org-babel-number-p (string)
  1256. "Return t if STRING represents a number."
  1257. (if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string)
  1258. (= (length (substring string (match-beginning 0)
  1259. (match-end 0)))
  1260. (length string)))
  1261. (string-to-number string)))
  1262. (defun org-babel-import-elisp-from-file (file-name)
  1263. "Read the results located at FILE-NAME into an elisp table. If
  1264. the table is trivial, then return it as a scalar."
  1265. (let (result)
  1266. (save-window-excursion
  1267. (with-temp-buffer
  1268. (condition-case nil
  1269. (progn
  1270. (org-table-import file-name nil)
  1271. (delete-file file-name)
  1272. (setq result (mapcar (lambda (row)
  1273. (mapcar #'org-babel-string-read row))
  1274. (org-table-to-lisp))))
  1275. (error nil)))
  1276. (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
  1277. (if (consp (car result))
  1278. (if (null (cdr (car result)))
  1279. (caar result)
  1280. result)
  1281. (car result))
  1282. result))))
  1283. (defun org-babel-string-read (cell)
  1284. "Strip nested \"s from around strings."
  1285. (org-babel-read (or (and (stringp cell)
  1286. (string-match "\\\"\\(.+\\)\\\"" cell)
  1287. (match-string 1 cell))
  1288. cell)))
  1289. (defun org-babel-reverse-string (string)
  1290. "Return the reverse of STRING."
  1291. (apply 'string (reverse (string-to-list string))))
  1292. (defun org-babel-chomp (string &optional regexp)
  1293. "Remove any trailing space or carriage returns characters from
  1294. STRING. Default regexp used is \"[ \f\t\n\r\v]\" but can be
  1295. overwritten by specifying a regexp as a second argument."
  1296. (let ((regexp (or regexp "[ \f\t\n\r\v]")))
  1297. (while (and (> (length string) 0)
  1298. (string-match regexp (substring string -1)))
  1299. (setq string (substring string 0 -1)))
  1300. string))
  1301. (defun org-babel-trim (string &optional regexp)
  1302. "Like `org-babel-chomp' only it runs on both the front and back
  1303. of the string."
  1304. (org-babel-chomp (org-babel-reverse-string
  1305. (org-babel-chomp (org-babel-reverse-string string) regexp))
  1306. regexp))
  1307. (defvar org-babel-org-babel-call-process-region-original nil)
  1308. (defun org-babel-tramp-handle-call-process-region
  1309. (start end program &optional delete buffer display &rest args)
  1310. "Use tramp to handle call-process-region. Fixes a bug in
  1311. `tramp-handle-call-process-region'."
  1312. (if (and (featurep 'tramp) (file-remote-p default-directory))
  1313. (let ((tmpfile (tramp-compat-make-temp-file "")))
  1314. (write-region start end tmpfile)
  1315. (when delete (delete-region start end))
  1316. (unwind-protect
  1317. ;; (apply 'call-process program tmpfile buffer display args)
  1318. ;; bug in tramp
  1319. (apply 'process-file program tmpfile buffer display args)
  1320. (delete-file tmpfile)))
  1321. ;; org-babel-call-process-region-original is the original emacs definition. It
  1322. ;; is in scope from the let binding in org-babel-execute-src-block
  1323. (apply org-babel-call-process-region-original
  1324. start end program delete buffer display args)))
  1325. (defun org-babel-maybe-remote-file (file)
  1326. "If FILE specifies a remove file, then parse the information on
  1327. the remote connection."
  1328. (if (file-remote-p default-directory)
  1329. (let* ((vec (tramp-dissect-file-name default-directory))
  1330. (user (tramp-file-name-user vec))
  1331. (host (tramp-file-name-host vec)))
  1332. (concat "/" user (when user "@") host ":" file))
  1333. file))
  1334. (defun org-babel-shell-command-on-region (start end command
  1335. &optional output-buffer replace
  1336. error-buffer display-error-buffer)
  1337. "Execute string COMMAND in inferior shell with region as input.
  1338. Fixes bugs in the emacs 23.1.1 version of `shell-command-on-region'
  1339. Normally display output (if any) in temp buffer `*Shell Command Output*';
  1340. Prefix arg means replace the region with it. Return the exit code of
  1341. COMMAND.
  1342. To specify a coding system for converting non-ASCII characters in
  1343. the input and output to the shell command, use
  1344. \\[universal-coding-system-argument] before this command. By
  1345. default, the input (from the current buffer) is encoded in the
  1346. same coding system that will be used to save the file,
  1347. `buffer-file-coding-system'. If the output is going to replace
  1348. the region, then it is decoded from that same coding system.
  1349. The noninteractive arguments are START, END, COMMAND,
  1350. OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER.
  1351. Noninteractive callers can specify coding systems by binding
  1352. `coding-system-for-read' and `coding-system-for-write'.
  1353. If the command generates output, the output may be displayed
  1354. in the echo area or in a buffer.
  1355. If the output is short enough to display in the echo area
  1356. \(determined by the variable `max-mini-window-height' if
  1357. `resize-mini-windows' is non-nil), it is shown there. Otherwise
  1358. it is displayed in the buffer `*Shell Command Output*'. The output
  1359. is available in that buffer in both cases.
  1360. If there is output and an error, a message about the error
  1361. appears at the end of the output.
  1362. If there is no output, or if output is inserted in the current buffer,
  1363. then `*Shell Command Output*' is deleted.
  1364. If the optional fourth argument OUTPUT-BUFFER is non-nil,
  1365. that says to put the output in some other buffer.
  1366. If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
  1367. If OUTPUT-BUFFER is not a buffer and not nil,
  1368. insert output in the current buffer.
  1369. In either case, the output is inserted after point (leaving mark after it).
  1370. If REPLACE, the optional fifth argument, is non-nil, that means insert
  1371. the output in place of text from START to END, putting point and mark
  1372. around it.
  1373. If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
  1374. or buffer name to which to direct the command's standard error output.
  1375. If it is nil, error output is mingled with regular output.
  1376. If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
  1377. were any errors. (This is always t, interactively.)
  1378. In an interactive call, the variable `shell-command-default-error-buffer'
  1379. specifies the value of ERROR-BUFFER."
  1380. (interactive (let (string)
  1381. (unless (mark)
  1382. (error "The mark is not set now, so there is no region"))
  1383. ;; Do this before calling region-beginning
  1384. ;; and region-end, in case subprocess output
  1385. ;; relocates them while we are in the minibuffer.
  1386. (setq string (read-shell-command "Shell command on region: "))
  1387. ;; call-interactively recognizes region-beginning and
  1388. ;; region-end specially, leaving them in the history.
  1389. (list (region-beginning) (region-end)
  1390. string
  1391. current-prefix-arg
  1392. current-prefix-arg
  1393. shell-command-default-error-buffer
  1394. t)))
  1395. (let ((error-file
  1396. (if error-buffer
  1397. (make-temp-file
  1398. (expand-file-name "scor"
  1399. (or (unless (featurep 'xemacs)
  1400. small-temporary-file-directory)
  1401. temporary-file-directory)))
  1402. nil))
  1403. exit-status)
  1404. (if (or replace
  1405. (and output-buffer
  1406. (not (or (bufferp output-buffer) (stringp output-buffer)))))
  1407. ;; Replace specified region with output from command.
  1408. (let ((swap (and replace (< start end))))
  1409. ;; Don't muck with mark unless REPLACE says we should.
  1410. (goto-char start)
  1411. (and replace (push-mark (point) 'nomsg))
  1412. (setq exit-status
  1413. (call-process-region start end shell-file-name t
  1414. (if error-file
  1415. (list output-buffer error-file)
  1416. t)
  1417. nil shell-command-switch command))
  1418. ;; It is rude to delete a buffer which the command is not using.
  1419. ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
  1420. ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
  1421. ;; (kill-buffer shell-buffer)))
  1422. ;; Don't muck with mark unless REPLACE says we should.
  1423. (and replace swap (exchange-point-and-mark)))
  1424. ;; No prefix argument: put the output in a temp buffer,
  1425. ;; replacing its entire contents.
  1426. (let ((buffer (get-buffer-create
  1427. (or output-buffer "*Shell Command Output*"))))
  1428. (unwind-protect
  1429. (if (eq buffer (current-buffer))
  1430. ;; If the input is the same buffer as the output,
  1431. ;; delete everything but the specified region,
  1432. ;; then replace that region with the output.
  1433. (progn (setq buffer-read-only nil)
  1434. (delete-region (max start end) (point-max))
  1435. (delete-region (point-min) (min start end))
  1436. (setq exit-status
  1437. (call-process-region (point-min) (point-max)
  1438. shell-file-name t
  1439. (if error-file
  1440. (list t error-file)
  1441. t)
  1442. nil shell-command-switch
  1443. command)))
  1444. ;; Clear the output buffer, then run the command with
  1445. ;; output there.
  1446. (let ((directory default-directory))
  1447. (save-current-buffer
  1448. (set-buffer buffer)
  1449. (setq buffer-read-only nil)
  1450. (if (not output-buffer)
  1451. (setq default-directory directory))
  1452. (erase-buffer)))
  1453. (setq exit-status
  1454. (call-process-region start end shell-file-name nil
  1455. (if error-file
  1456. (list buffer error-file)
  1457. buffer)
  1458. nil shell-command-switch command)))
  1459. ;; Report the output.
  1460. (with-current-buffer buffer
  1461. (setq mode-line-process
  1462. (cond ((null exit-status)
  1463. " - Error")
  1464. ((stringp exit-status)
  1465. (format " - Signal [%s]" exit-status))
  1466. ((not (equal 0 exit-status))
  1467. (format " - Exit [%d]" exit-status)))))
  1468. (if (with-current-buffer buffer (> (point-max) (point-min)))
  1469. ;; There's some output, display it
  1470. (display-message-or-buffer buffer)
  1471. ;; No output; error?
  1472. (let ((output
  1473. (if (and error-file
  1474. (< 0 (nth 7 (file-attributes error-file))))
  1475. "some error output"
  1476. "no output")))
  1477. (cond ((null exit-status)
  1478. (message "(Shell command failed with error)"))
  1479. ((equal 0 exit-status)
  1480. (message "(Shell command succeeded with %s)"
  1481. output))
  1482. ((stringp exit-status)
  1483. (message "(Shell command killed by signal %s)"
  1484. exit-status))
  1485. (t
  1486. (message "(Shell command failed with code %d and %s)"
  1487. exit-status output))))
  1488. ;; Don't kill: there might be useful info in the undo-log.
  1489. ;; (kill-buffer buffer)
  1490. ))))
  1491. (when (and error-file (file-exists-p error-file))
  1492. (if (< 0 (nth 7 (file-attributes error-file)))
  1493. (with-current-buffer (get-buffer-create error-buffer)
  1494. (let ((pos-from-end (- (point-max) (point))))
  1495. (or (bobp)
  1496. (insert "\f\n"))
  1497. ;; Do no formatting while reading error file,
  1498. ;; because that can run a shell command, and we
  1499. ;; don't want that to cause an infinite recursion.
  1500. (format-insert-file error-file nil)
  1501. ;; Put point after the inserted errors.
  1502. (goto-char (- (point-max) pos-from-end)))
  1503. (and display-error-buffer
  1504. (display-buffer (current-buffer)))))
  1505. (delete-file error-file))
  1506. exit-status))
  1507. (provide 'ob)
  1508. ;;; ob.el ends here