ob-eval.el 6.1 KB

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