ob-eval.el 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. ;;; ob-eval.el --- org-babel functions for external code evaluation
  2. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, comint
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; These functions build existing Emacs support for executing external
  19. ;; shell commands.
  20. ;;; Code:
  21. (eval-when-compile (require 'cl))
  22. (defvar org-babel-error-buffer-name "*Org-Babel Error Output*")
  23. (defun org-babel-eval-error-notify (exit-code stderr)
  24. "Open a buffer to display STDERR and a message with the value of EXIT-CODE."
  25. (let ((buf (get-buffer-create org-babel-error-buffer-name)))
  26. (with-current-buffer buf
  27. (goto-char (point-max))
  28. (save-excursion (insert stderr)))
  29. (display-buffer buf))
  30. (message "Babel evaluation exited with code %S" exit-code))
  31. (defun org-babel-eval (cmd body)
  32. "Run CMD on BODY.
  33. If CMD succeeds then return its results, otherwise display
  34. STDERR with `org-babel-eval-error-notify'."
  35. (let ((err-buff (get-buffer-create " *Org-Babel Error*")) exit-code)
  36. (with-current-buffer err-buff (erase-buffer))
  37. (with-temp-buffer
  38. (insert body)
  39. (setq exit-code
  40. (org-babel-shell-command-on-region
  41. (point-min) (point-max) cmd t 'replace err-buff))
  42. (if (or (not (numberp exit-code)) (> exit-code 0))
  43. (progn
  44. (with-current-buffer err-buff
  45. (org-babel-eval-error-notify exit-code (buffer-string)))
  46. nil)
  47. (buffer-string)))))
  48. (defun org-babel-eval-read-file (file)
  49. "Return the contents of FILE as a string."
  50. (with-temp-buffer (insert-file-contents file)
  51. (buffer-string)))
  52. (defun org-babel-shell-command-on-region (start end command
  53. &optional output-buffer replace
  54. error-buffer display-error-buffer)
  55. "Execute COMMAND in an inferior shell with region as input.
  56. Fixes bugs in the emacs 23.1.1 version of `shell-command-on-region'
  57. Normally display output (if any) in temp buffer `*Shell Command Output*';
  58. Prefix arg means replace the region with it. Return the exit code of
  59. COMMAND.
  60. To specify a coding system for converting non-ASCII characters in
  61. the input and output to the shell command, use
  62. \\[universal-coding-system-argument] before this command. By
  63. default, the input (from the current buffer) is encoded in the
  64. same coding system that will be used to save the file,
  65. `buffer-file-coding-system'. If the output is going to replace
  66. the region, then it is decoded from that same coding system.
  67. The noninteractive arguments are START, END, COMMAND,
  68. OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER.
  69. Noninteractive callers can specify coding systems by binding
  70. `coding-system-for-read' and `coding-system-for-write'.
  71. If the command generates output, the output may be displayed
  72. in the echo area or in a buffer.
  73. If the output is short enough to display in the echo area
  74. \(determined by the variable `max-mini-window-height' if
  75. `resize-mini-windows' is non-nil), it is shown there. Otherwise
  76. it is displayed in the buffer `*Shell Command Output*'. The output
  77. is available in that buffer in both cases.
  78. If there is output and an error, a message about the error
  79. appears at the end of the output.
  80. If there is no output, or if output is inserted in the current buffer,
  81. then `*Shell Command Output*' is deleted.
  82. If the optional fourth argument OUTPUT-BUFFER is non-nil,
  83. that says to put the output in some other buffer.
  84. If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
  85. If OUTPUT-BUFFER is not a buffer and not nil,
  86. insert output in the current buffer.
  87. In either case, the output is inserted after point (leaving mark after it).
  88. If REPLACE, the optional fifth argument, is non-nil, that means insert
  89. the output in place of text from START to END, putting point and mark
  90. around it.
  91. If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
  92. or buffer name to which to direct the command's standard error output.
  93. If it is nil, error output is mingled with regular output.
  94. If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
  95. were any errors. (This is always t, interactively.)
  96. In an interactive call, the variable `shell-command-default-error-buffer'
  97. specifies the value of ERROR-BUFFER."
  98. (interactive (let (string)
  99. (unless (mark)
  100. (error "The mark is not set now, so there is no region"))
  101. ;; Do this before calling region-beginning
  102. ;; and region-end, in case subprocess output
  103. ;; relocates them while we are in the minibuffer.
  104. (setq string (read-shell-command "Shell command on region: "))
  105. ;; call-interactively recognizes region-beginning and
  106. ;; region-end specially, leaving them in the history.
  107. (list (region-beginning) (region-end)
  108. string
  109. current-prefix-arg
  110. current-prefix-arg
  111. shell-command-default-error-buffer
  112. t)))
  113. (let ((error-file
  114. (if error-buffer
  115. (make-temp-file
  116. (expand-file-name "scor"
  117. (if (featurep 'xemacs)
  118. (temp-directory)
  119. temporary-file-directory)))
  120. nil))
  121. exit-status)
  122. (if (or replace
  123. (and output-buffer
  124. (not (or (bufferp output-buffer) (stringp output-buffer)))))
  125. ;; Replace specified region with output from command.
  126. (let ((swap (and replace (< start end))))
  127. ;; Don't muck with mark unless REPLACE says we should.
  128. (goto-char start)
  129. (and replace (push-mark (point) 'nomsg))
  130. (setq exit-status
  131. (call-process-region start end shell-file-name t
  132. (if error-file
  133. (list output-buffer error-file)
  134. t)
  135. nil shell-command-switch command))
  136. ;; It is rude to delete a buffer which the command is not using.
  137. ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
  138. ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
  139. ;; (kill-buffer shell-buffer)))
  140. ;; Don't muck with mark unless REPLACE says we should.
  141. (and replace swap (exchange-point-and-mark)))
  142. ;; No prefix argument: put the output in a temp buffer,
  143. ;; replacing its entire contents.
  144. (let ((buffer (get-buffer-create
  145. (or output-buffer "*Shell Command Output*"))))
  146. (unwind-protect
  147. (if (eq buffer (current-buffer))
  148. ;; If the input is the same buffer as the output,
  149. ;; delete everything but the specified region,
  150. ;; then replace that region with the output.
  151. (progn (setq buffer-read-only nil)
  152. (delete-region (max start end) (point-max))
  153. (delete-region (point-min) (min start end))
  154. (setq exit-status
  155. (call-process-region (point-min) (point-max)
  156. shell-file-name t
  157. (if error-file
  158. (list t error-file)
  159. t)
  160. nil shell-command-switch
  161. command)))
  162. ;; Clear the output buffer, then run the command with
  163. ;; output there.
  164. (let ((directory default-directory))
  165. (with-current-buffer buffer
  166. (setq buffer-read-only nil)
  167. (if (not output-buffer)
  168. (setq default-directory directory))
  169. (erase-buffer)))
  170. (setq exit-status
  171. (call-process-region start end shell-file-name nil
  172. (if error-file
  173. (list buffer error-file)
  174. buffer)
  175. nil shell-command-switch command)))
  176. ;; Report the output.
  177. (with-current-buffer buffer
  178. (setq mode-line-process
  179. (cond ((null exit-status)
  180. " - Error")
  181. ((stringp exit-status)
  182. (format " - Signal [%s]" exit-status))
  183. ((not (equal 0 exit-status))
  184. (format " - Exit [%d]" exit-status)))))
  185. (if (with-current-buffer buffer (> (point-max) (point-min)))
  186. ;; There's some output, display it
  187. (display-message-or-buffer buffer)
  188. ;; No output; error?
  189. (let ((output
  190. (if (and error-file
  191. (< 0 (nth 7 (file-attributes error-file))))
  192. "some error output"
  193. "no output")))
  194. (cond ((null exit-status)
  195. (message "(Shell command failed with error)"))
  196. ((equal 0 exit-status)
  197. (message "(Shell command succeeded with %s)"
  198. output))
  199. ((stringp exit-status)
  200. (message "(Shell command killed by signal %s)"
  201. exit-status))
  202. (t
  203. (message "(Shell command failed with code %d and %s)"
  204. exit-status output))))
  205. ;; Don't kill: there might be useful info in the undo-log.
  206. ;; (kill-buffer buffer)
  207. ))))
  208. (when (and error-file (file-exists-p error-file))
  209. (if (< 0 (nth 7 (file-attributes error-file)))
  210. (with-current-buffer (get-buffer-create error-buffer)
  211. (let ((pos-from-end (- (point-max) (point))))
  212. (or (bobp)
  213. (insert "\f\n"))
  214. ;; Do no formatting while reading error file,
  215. ;; because that can run a shell command, and we
  216. ;; don't want that to cause an infinite recursion.
  217. (format-insert-file error-file nil)
  218. ;; Put point after the inserted errors.
  219. (goto-char (- (point-max) pos-from-end)))
  220. (and display-error-buffer
  221. (display-buffer (current-buffer)))))
  222. (delete-file error-file))
  223. exit-status))
  224. (defun org-babel-eval-wipe-error-buffer ()
  225. "Delete the contents of the Org code block error buffer.
  226. This buffer is named by `org-babel-error-buffer-name'."
  227. (when (get-buffer org-babel-error-buffer-name)
  228. (with-current-buffer org-babel-error-buffer-name
  229. (delete-region (point-min) (point-max)))))
  230. (provide 'ob-eval)
  231. ;;; ob-eval.el ends here