ob-eshell.el 3.5 KB

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