ob-eval.el 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ;;; ob-eval.el --- Babel Functions for External Code Evaluation -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, comint
  5. ;; URL: 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. (org-assert-version)
  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 (command query)
  34. "Run COMMAND on QUERY.
  35. Writes QUERY into a temp-buffer that is processed with
  36. `org-babel--shell-command-on-region'. If COMMAND succeeds then return
  37. its results, otherwise display STDERR with
  38. `org-babel-eval-error-notify'."
  39. (let ((error-buffer (get-buffer-create " *Org-Babel Error*")) exit-code)
  40. (with-current-buffer error-buffer (erase-buffer))
  41. (with-temp-buffer
  42. (insert query)
  43. (setq exit-code
  44. (org-babel--shell-command-on-region
  45. command error-buffer))
  46. (if (or (not (numberp exit-code)) (> exit-code 0))
  47. (progn
  48. (with-current-buffer error-buffer
  49. (org-babel-eval-error-notify exit-code (buffer-string)))
  50. (save-excursion
  51. (when (get-buffer org-babel-error-buffer-name)
  52. (with-current-buffer org-babel-error-buffer-name
  53. (unless (derived-mode-p 'compilation-mode)
  54. (compilation-mode))
  55. ;; Compilation-mode enforces read-only, but Babel expects the buffer modifiable.
  56. (setq buffer-read-only nil))))
  57. nil)
  58. (buffer-string)))))
  59. (defun org-babel-eval-read-file (file)
  60. "Return the contents of FILE as a string."
  61. (with-temp-buffer (insert-file-contents file)
  62. (buffer-string)))
  63. (defun org-babel--shell-command-on-region (command error-buffer)
  64. "Execute COMMAND in an inferior shell with region as input.
  65. Stripped down version of `shell-command-on-region' for internal use in
  66. Babel only. This lets us work around errors in the original function
  67. in various versions of Emacs. This expects the query to be run to be
  68. in the current temp buffer. This is written into
  69. input-file. ERROR-BUFFER is the name of the file which
  70. `org-babel-eval' has created to use for any error messages that are
  71. returned."
  72. (let ((input-file (org-babel-temp-file "ob-input-"))
  73. (error-file (if error-buffer (org-babel-temp-file "ob-error-") nil))
  74. (shell-file-name (org-babel--get-shell-file-name))
  75. exit-status)
  76. ;; There is an error in `process-file' when `error-file' exists.
  77. ;; This is fixed in Emacs trunk as of 2012-12-21; let's use this
  78. ;; workaround for now.
  79. (unless (file-remote-p default-directory)
  80. (delete-file error-file))
  81. ;; we always call this with 'replace, remove conditional
  82. ;; Replace specified region with output from command.
  83. (org-babel--write-temp-buffer-input-file input-file)
  84. (setq exit-status
  85. (process-file shell-file-name input-file
  86. (if error-file
  87. (list t error-file)
  88. t)
  89. nil shell-command-switch command))
  90. (when (and input-file (file-exists-p input-file)
  91. ;; bind org-babel--debug-input around the call to keep
  92. ;; the temporary input files available for inspection
  93. (not (when (boundp 'org-babel--debug-input)
  94. org-babel--debug-input)))
  95. (delete-file input-file))
  96. (when (and error-file (file-exists-p error-file))
  97. (when (< 0 (file-attribute-size (file-attributes error-file)))
  98. (with-current-buffer (get-buffer-create error-buffer)
  99. (let ((pos-from-end (- (point-max) (point))))
  100. (or (bobp)
  101. (insert "\f\n"))
  102. ;; Do no formatting while reading error file,
  103. ;; because that can run a shell command, and we
  104. ;; don't want that to cause an infinite recursion.
  105. (format-insert-file error-file nil)
  106. ;; Put point after the inserted errors.
  107. (goto-char (- (point-max) pos-from-end)))
  108. (current-buffer)))
  109. (delete-file error-file))
  110. exit-status))
  111. (defun org-babel--write-temp-buffer-input-file (input-file)
  112. "Write the contents of the current temp buffer into INPUT-FILE."
  113. (let ((start (point-min))
  114. (end (point-max)))
  115. (goto-char start)
  116. (push-mark (point) 'nomsg)
  117. (write-region start end input-file)
  118. (delete-region start end)
  119. (exchange-point-and-mark)))
  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. (defun org-babel--get-shell-file-name ()
  127. "Return system `shell-file-name', defaulting to /bin/sh.
  128. Unfortunately, `executable-find' does not support file name
  129. handlers. Therefore, we could use it in the local case only."
  130. ;; FIXME: This is generic enough that it should probably be in emacs, not org-mode
  131. (cond ((and (not (file-remote-p default-directory))
  132. (executable-find shell-file-name))
  133. shell-file-name)
  134. ((file-executable-p
  135. (concat (file-remote-p default-directory) shell-file-name))
  136. shell-file-name)
  137. ("/bin/sh")))
  138. (provide 'ob-eval)
  139. ;;; ob-eval.el ends here