ob-eval.el 5.4 KB

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