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