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