ob-eval.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ;;; ob-eval.el --- Babel Functions for External Code Evaluation -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2015 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. (save-excursion
  49. (when (get-buffer org-babel-error-buffer-name)
  50. (with-current-buffer org-babel-error-buffer-name
  51. (unless (derived-mode-p 'compilation-mode)
  52. (compilation-mode))
  53. ;; Compilation-mode enforces read-only, but Babel expects the buffer modifiable.
  54. (setq buffer-read-only nil))))
  55. nil)
  56. (buffer-string)))))
  57. (defun org-babel-eval-read-file (file)
  58. "Return the contents of FILE as a string."
  59. (with-temp-buffer (insert-file-contents file)
  60. (buffer-string)))
  61. (defun org-babel--shell-command-on-region (start end command error-buffer)
  62. "Execute COMMAND in an inferior shell with region as input.
  63. Stripped down version of shell-command-on-region for internal use
  64. in Babel only. This lets us work around errors in the original
  65. function in various versions of Emacs.
  66. "
  67. (let ((input-file (org-babel-temp-file "ob-input-"))
  68. (error-file (if error-buffer (org-babel-temp-file "ob-error-") nil))
  69. ;; Unfortunately, `executable-find' does not support file name
  70. ;; handlers. Therefore, we could use it in the local case
  71. ;; only.
  72. (shell-file-name
  73. (cond ((and (not (file-remote-p default-directory))
  74. (executable-find shell-file-name))
  75. shell-file-name)
  76. ((file-executable-p
  77. (concat (file-remote-p default-directory) shell-file-name))
  78. shell-file-name)
  79. ("/bin/sh")))
  80. exit-status)
  81. ;; There is an error in `process-file' when `error-file' exists.
  82. ;; This is fixed in Emacs trunk as of 2012-12-21; let's use this
  83. ;; workaround for now.
  84. (unless (file-remote-p default-directory)
  85. (delete-file error-file))
  86. ;; we always call this with 'replace, remove conditional
  87. ;; Replace specified region with output from command.
  88. (let ((swap (< start end)))
  89. (goto-char start)
  90. (push-mark (point) 'nomsg)
  91. (write-region start end input-file)
  92. (delete-region start end)
  93. (setq exit-status
  94. (process-file shell-file-name input-file
  95. (if error-file
  96. (list t error-file)
  97. t)
  98. nil shell-command-switch command))
  99. (when swap (exchange-point-and-mark)))
  100. (when (and input-file (file-exists-p input-file)
  101. ;; bind org-babel--debug-input around the call to keep
  102. ;; the temporary input files available for inspection
  103. (not (when (boundp 'org-babel--debug-input)
  104. org-babel--debug-input)))
  105. (delete-file input-file))
  106. (when (and error-file (file-exists-p error-file))
  107. (if (< 0 (nth 7 (file-attributes error-file)))
  108. (with-current-buffer (get-buffer-create error-buffer)
  109. (let ((pos-from-end (- (point-max) (point))))
  110. (or (bobp)
  111. (insert "\f\n"))
  112. ;; Do no formatting while reading error file,
  113. ;; because that can run a shell command, and we
  114. ;; don't want that to cause an infinite recursion.
  115. (format-insert-file error-file nil)
  116. ;; Put point after the inserted errors.
  117. (goto-char (- (point-max) pos-from-end)))
  118. (current-buffer)))
  119. (delete-file error-file))
  120. exit-status))
  121. (defun org-babel-eval-wipe-error-buffer ()
  122. "Delete the contents of the Org code block error buffer.
  123. This buffer is named by `org-babel-error-buffer-name'."
  124. (when (get-buffer org-babel-error-buffer-name)
  125. (with-current-buffer org-babel-error-buffer-name
  126. (delete-region (point-min) (point-max)))))
  127. (provide 'ob-eval)
  128. ;;; ob-eval.el ends here