ob-eshell.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;;; ob-eshell.el --- Babel Functions for Eshell -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2018-2022 Free Software Foundation, Inc.
  3. ;; Author: stardiviner <numbchild@gmail.com>
  4. ;; Maintainer: stardiviner <numbchild@gmail.com>
  5. ;; URL: https://github.com/stardiviner/ob-eshell
  6. ;; Keywords: literate programming, reproducible research
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org Babel support for evaluating Eshell source code.
  20. ;;; Code:
  21. (require 'org-macs)
  22. (org-assert-version)
  23. (require 'ob)
  24. (require 'eshell)
  25. (declare-function eshell-send-input "esh-mode"
  26. (&optional use-region queue-p no-newline))
  27. (defvar eshell-last-output-start)
  28. (defvar eshell-last-output-end)
  29. (defvar eshell-last-input-end)
  30. (defvar org-babel-default-header-args:eshell '())
  31. (defun org-babel-execute:eshell (body params)
  32. "Execute a block of Eshell code BODY with PARAMS.
  33. This function is called by `org-babel-execute-src-block'.
  34. The BODY can be any code which allowed executed in Eshell.
  35. Eshell allow to execute normal shell command and Elisp code.
  36. More details please reference Eshell Info.
  37. The PARAMS are variables assignments."
  38. (let* ((session (org-babel-eshell-initiate-session
  39. (cdr (assq :session params))))
  40. (full-body (org-babel-expand-body:generic
  41. body params (org-babel-variable-assignments:eshell params))))
  42. (if session
  43. (progn
  44. (with-current-buffer session
  45. (dolist (line (split-string full-body "\n"))
  46. (goto-char eshell-last-output-end)
  47. (insert line)
  48. (eshell-send-input))
  49. ;; get output of last input
  50. ;; TODO: collect all output instead of last command's output.
  51. (goto-char eshell-last-input-end)
  52. (buffer-substring-no-properties (point) eshell-last-output-start)))
  53. (with-temp-buffer
  54. (eshell-command full-body t)
  55. (buffer-string)))))
  56. (defun org-babel-prep-session:eshell (session params)
  57. "Prepare SESSION according to the header arguments specified in PARAMS."
  58. (let* ((session (org-babel-eshell-initiate-session session))
  59. ;; Eshell session buffer is read from variable `eshell-buffer-name'.
  60. (eshell-buffer-name session)
  61. (var-lines (org-babel-variable-assignments:eshell params)))
  62. (call-interactively #'eshell)
  63. (mapc #'eshell-command var-lines)
  64. session))
  65. (defun ob-eshell-session-live-p (session)
  66. "Non-nil if Eshell SESSION exists."
  67. (get-buffer session))
  68. (defun org-babel-eshell-initiate-session (&optional session _params)
  69. "Initiate a session named SESSION."
  70. (when (and session (not (string= session "none")))
  71. (save-window-excursion
  72. (unless (ob-eshell-session-live-p session)
  73. (let ((eshell-buffer-name session)) (eshell))))
  74. session))
  75. (defun org-babel-variable-assignments:eshell (params)
  76. "Convert ob-eshell :var specified variables into Eshell variables assignments."
  77. (mapcar
  78. (lambda (pair)
  79. (format "(setq %s %S)" (car pair) (cdr pair)))
  80. (org-babel--get-vars params)))
  81. (defun org-babel-load-session:eshell (session body params)
  82. "Load BODY into SESSION with PARAMS."
  83. (save-window-excursion
  84. (let ((buffer (org-babel-prep-session:eshell session params)))
  85. (with-current-buffer buffer
  86. (goto-char (point-max))
  87. (insert (org-babel-chomp body)))
  88. buffer)))
  89. (provide 'ob-eshell)
  90. ;;; ob-eshell.el ends here