ob-eshell.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ;;; ob-eshell.el --- Babel Functions for Eshell -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2018-2020 Free Software Foundation, Inc.
  3. ;; Author: stardiviner <numbchild@gmail.com>
  4. ;; Keywords: literate programming, reproducible research
  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. ;; Org Babel support for evaluating Eshell source code.
  19. ;;; Code:
  20. (require 'ob)
  21. (require 'eshell)
  22. (declare-function eshell-send-input "esh-mode"
  23. (&optional use-region queue-p no-newline))
  24. (defvar eshell-last-output-start)
  25. (defvar eshell-last-output-end)
  26. (defvar eshell-last-input-end)
  27. (defvar org-babel-default-header-args:eshell '())
  28. (defun org-babel-execute:eshell (body params)
  29. "Execute a block of Eshell code BODY with PARAMS.
  30. This function is called by `org-babel-execute-src-block'.
  31. The BODY can be any code which allowed executed in Eshell.
  32. Eshell allow to execute normal shell command and Elisp code.
  33. More details please reference Eshell Info.
  34. The PARAMS are variables assignments."
  35. (let* ((session (org-babel-eshell-initiate-session
  36. (cdr (assq :session params))))
  37. (full-body (org-babel-expand-body:generic
  38. body params (org-babel-variable-assignments:eshell params))))
  39. (if session
  40. (progn
  41. (with-current-buffer session
  42. (dolist (line (split-string full-body "\n"))
  43. (goto-char eshell-last-output-end)
  44. (insert line)
  45. (eshell-send-input))
  46. ;; get output of last input
  47. ;; TODO: collect all output instead of last command's output.
  48. (goto-char eshell-last-input-end)
  49. (buffer-substring-no-properties (point) eshell-last-output-start)))
  50. (with-temp-buffer
  51. (eshell-command full-body t)
  52. (buffer-string)))))
  53. (defun org-babel-prep-session:eshell (session params)
  54. "Prepare SESSION according to the header arguments specified in PARAMS."
  55. (let* ((session (org-babel-eshell-initiate-session session))
  56. ;; Eshell session buffer is read from variable `eshell-buffer-name'.
  57. (eshell-buffer-name session)
  58. (var-lines (org-babel-variable-assignments:eshell params)))
  59. (call-interactively #'eshell)
  60. (mapc #'eshell-command var-lines)
  61. session))
  62. (defun ob-eshell-session-live-p (session)
  63. "Non-nil if Eshell SESSION exists."
  64. (get-buffer session))
  65. (defun org-babel-eshell-initiate-session (&optional session _params)
  66. "Initiate a session named SESSION."
  67. (when (and session (not (string= session "none")))
  68. (save-window-excursion
  69. (unless (ob-eshell-session-live-p session)
  70. (let ((eshell-buffer-name session)) (eshell))))
  71. session))
  72. (defun org-babel-variable-assignments:eshell (params)
  73. "Convert ob-eshell :var specified variables into Eshell variables assignments."
  74. (mapcar
  75. (lambda (pair)
  76. (format "(setq %s %S)" (car pair) (cdr pair)))
  77. (org-babel--get-vars params)))
  78. (defun org-babel-load-session:eshell (session body params)
  79. "Load BODY into SESSION with PARAMS."
  80. (save-window-excursion
  81. (let ((buffer (org-babel-prep-session:eshell session params)))
  82. (with-current-buffer buffer
  83. (goto-char (point-max))
  84. (insert (org-babel-chomp body)))
  85. buffer)))
  86. (provide 'ob-eshell)
  87. ;;; ob-eshell.el ends here