emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob f1cd859a4c1aaf799e3488af725bfe0b050878fc 3690 bytes (raw)
name: lisp/ob-eshell.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
 
;;; ob-eshell.el --- Babel Functions for Eshell            -*- lexical-binding: t; -*-

;; Copyright (C) 2009-2018 Free Software Foundation, Inc.

;; Author: stardiviner <numbchild@gmail.com>
;; Keywords: literate programming, reproducible research
;; Homepage: https://orgmode.org

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Org Babel support for evaluating Eshell source code.

;;; Code:
(require 'ob)
(require 'eshell)

(defvar org-babel-default-header-args:eshell '())

(defun org-babel-execute:eshell (body params)
  "Execute a block of Eshell code BODY with PARAMS.
This function is called by `org-babel-execute-src-block'.

The BODY can be any code which allowed executed in Eshell.
Eshell allow to execute normal shell command and Elisp code.
More details please reference Eshell Info.

The PARAMS are variables assignments."
  (let* ((session (org-babel-eshell-initiate-session
		   (cdr (assq :session params))))
	 (full-body (org-babel-expand-body:generic
		     body params (org-babel-variable-assignments:eshell params))))
    (if session
	(progn
	  (with-current-buffer session
	    (dolist (line (split-string full-body "\n"))
	      (goto-char eshell-last-output-end)
	      (insert line)
	      (eshell-send-input))
	    ;; get output of last input
	    ;; TODO: collect all output instead of last command's output.
	    (goto-char eshell-last-input-end)
	    (buffer-substring-no-properties (point) eshell-last-output-start)))
      (with-temp-buffer
	(eshell-command full-body t)
	(buffer-string)))))

(defun org-babel-prep-session:eshell (session params)
  "Prepare SESSION according to the header arguments specified in PARAMS."
  (let* ((session (org-babel-eshell-initiate-session session))
	 ;; Eshell session buffer is read from variable `eshell-buffer-name'.
	 (eshell-buffer-name session)
	 (var-lines (org-babel-variable-assignments:eshell params)))
    (call-interactively #'eshell)
    (mapc #'eshell-command var-lines)
    session))

(defun ob-eshell-session-live-p (session)
  "Non-nil if Eshell SESSION exists."
  (get-buffer session))

(defun org-babel-eshell-initiate-session (&optional session params)
  "Initiate a session named SESSION according to PARAMS."
  (when (and session (not (string= session "none")))
    (save-window-excursion
      (or (ob-eshell-session-live-p session)
	  (progn
	    (let ((eshell-buffer-name session))
	      (eshell))
	    (get-buffer (current-buffer)))))
    session))

(defun org-babel-variable-assignments:eshell (params)
  "Convert ob-eshell :var specified variables into Eshell variables assignments."
  (mapcar
   (lambda (pair)
     (format "(setq %s %S)" (car pair) (cdr pair)))
   (org-babel--get-vars params)))

(defun org-babel-load-session:eshell (session body params)
  "Load BODY into SESSION with PARAMS."
  (save-window-excursion
    (let ((buffer (org-babel-prep-session:eshell session params)))
      (with-current-buffer buffer
	(goto-char (point-max))
	(insert (org-babel-chomp body)))
      buffer)))

(provide 'ob-eshell)

;;; ob-eshell.el ends here

debug log:

solving f1cd859a4 ...
found f1cd859a4 in https://yhetil.org/orgmode/87sh7i2q88.fsf@gmail.com/

applying [1/1] https://yhetil.org/orgmode/87sh7i2q88.fsf@gmail.com/
diff --git a/lisp/ob-eshell.el b/lisp/ob-eshell.el
new file mode 100644
index 000000000..f1cd859a4

Checking patch lisp/ob-eshell.el...
Applied patch lisp/ob-eshell.el cleanly.

index at:
100644 f1cd859a4c1aaf799e3488af725bfe0b050878fc	lisp/ob-eshell.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).