ob-eshell.el 3.8 KB

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