ob-eval.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ;;; ob-eval.el --- org-babel functions for external code evaluation
  2. ;; Copyright (C) 2009-2014 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. (require 'org-macs)
  22. (eval-when-compile (require 'cl))
  23. (defvar org-babel-error-buffer-name "*Org-Babel Error Output*")
  24. (declare-function org-babel-temp-file "ob-core" (prefix &optional suffix))
  25. (defun org-babel-eval-error-notify (exit-code stderr)
  26. "Open a buffer to display STDERR and a message with the value of EXIT-CODE."
  27. (let ((buf (get-buffer-create org-babel-error-buffer-name)))
  28. (with-current-buffer buf
  29. (goto-char (point-max))
  30. (save-excursion (insert stderr)))
  31. (display-buffer buf))
  32. (message "Babel evaluation exited with code %S" exit-code))
  33. (defun org-babel-eval (cmd body)
  34. "Run CMD on BODY.
  35. If CMD succeeds then return its results, otherwise display
  36. STDERR with `org-babel-eval-error-notify'."
  37. (let ((err-buff (get-buffer-create " *Org-Babel Error*")) exit-code)
  38. (with-current-buffer err-buff (erase-buffer))
  39. (with-temp-buffer
  40. (insert body)
  41. (setq exit-code
  42. (org-babel--shell-command-on-region
  43. (point-min) (point-max) cmd err-buff))
  44. (if (or (not (numberp exit-code)) (> exit-code 0))
  45. (progn
  46. (with-current-buffer err-buff
  47. (org-babel-eval-error-notify exit-code (buffer-string)))
  48. nil)
  49. (buffer-string)))))
  50. (defun org-babel-eval-read-file (file)
  51. "Return the contents of FILE as a string."
  52. (with-temp-buffer (insert-file-contents file)
  53. (buffer-string)))
  54. (defun org-babel--shell-command-on-region (start end command error-buffer)
  55. "Execute COMMAND in an inferior shell with region as input.
  56. Stripped down version of shell-command-on-region for internal use
  57. in Babel only. This lets us work around errors in the original
  58. function in various versions of Emacs.
  59. "
  60. (let ((input-file (org-babel-temp-file "ob-input-"))
  61. (error-file (if error-buffer (org-babel-temp-file "ob-error-") nil))
  62. ;; Unfortunately, `executable-find' does not support file name
  63. ;; handlers. Therefore, we could use it in the local case
  64. ;; only.
  65. (shell-file-name
  66. (cond ((and (not (file-remote-p default-directory))
  67. (executable-find shell-file-name))
  68. shell-file-name)
  69. ((file-executable-p
  70. (concat (file-remote-p default-directory) shell-file-name))
  71. shell-file-name)
  72. ("/bin/sh")))
  73. exit-status)
  74. ;; There is an error in `process-file' when `error-file' exists.
  75. ;; This is fixed in Emacs trunk as of 2012-12-21; let's use this
  76. ;; workaround for now.
  77. (unless (file-remote-p default-directory)
  78. (delete-file error-file))
  79. ;; we always call this with 'replace, remove conditional
  80. ;; Replace specified region with output from command.
  81. (let ((swap (< start end)))
  82. (goto-char start)
  83. (push-mark (point) 'nomsg)
  84. (write-region start end input-file)
  85. (delete-region start end)
  86. (setq exit-status
  87. (process-file shell-file-name input-file
  88. (if error-file
  89. (list t error-file)
  90. t)
  91. nil shell-command-switch command))
  92. (when swap (exchange-point-and-mark)))
  93. (when (and input-file (file-exists-p input-file)
  94. ;; bind org-babel--debug-input around the call to keep
  95. ;; the temporary input files available for inspection
  96. (not (when (boundp 'org-babel--debug-input)
  97. org-babel--debug-input)))
  98. (delete-file input-file))
  99. (when (and error-file (file-exists-p error-file))
  100. (if (< 0 (nth 7 (file-attributes error-file)))
  101. (with-current-buffer (get-buffer-create error-buffer)
  102. (let ((pos-from-end (- (point-max) (point))))
  103. (or (bobp)
  104. (insert "\f\n"))
  105. ;; Do no formatting while reading error file,
  106. ;; because that can run a shell command, and we
  107. ;; don't want that to cause an infinite recursion.
  108. (format-insert-file error-file nil)
  109. ;; Put point after the inserted errors.
  110. (goto-char (- (point-max) pos-from-end)))
  111. (current-buffer)))
  112. (delete-file error-file))
  113. exit-status))
  114. (defun org-babel-eval-wipe-error-buffer ()
  115. "Delete the contents of the Org code block error buffer.
  116. This buffer is named by `org-babel-error-buffer-name'."
  117. (when (get-buffer org-babel-error-buffer-name)
  118. (with-current-buffer org-babel-error-buffer-name
  119. (delete-region (point-min) (point-max)))))
  120. (provide 'ob-eval)
  121. ;;; ob-eval.el ends here