ob-eval.el 5.1 KB

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