From: stardiviner <numbchild@gmail.com>
To: org-mode <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] Add support for Babel with Eshell, and some questions
Date: Sun, 22 Apr 2018 10:01:11 +0800 [thread overview]
Message-ID: <87bmecrod4.fsf@gmail.com> (raw)
In-Reply-To: <87d0ysroer.fsf@gmail.com>
[-- Attachment #1.1: Type: text/plain, Size: 33 bytes --]
Ahh, forgot include my patches.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: 0001-ob-eshell.el-Add-Eshell-support-for-Babel.patch --]
[-- Type: text/x-patch, Size: 7641 bytes --]
From 132c94b9b40fd9ba694fa4338f325582771c53ec Mon Sep 17 00:00:00 2001
From: stardiviner <numbchild@gmail.com>
Date: Sun, 22 Apr 2018 09:37:40 +0800
Subject: [PATCH] ob-eshell.el: Add Eshell support for Babel.
* lisp/ob-eshell.el (org-babel-execute:eshell): Execute Eshell code in Babel.
(org-babel-prep-session:eshell):
(ob-eshell-session-live-p):
(org-babel-eshell-initiate-session):
(org-babel-variable-assignments:eshell):
(org-babel-load-session:eshell):
* testing/test-ob-eshell.el: Write test for ob-eshell.
* doc/org-manual.org (Languages): Add document for ob-eshell.
---
doc/org-manual.org | 1 +
lisp/ob-eshell.el | 98 ++++++++++++++++++++++++++++++++++
testing/lisp/test-ob-eshell.el | 73 +++++++++++++++++++++++++
3 files changed, 172 insertions(+)
create mode 100644 lisp/ob-eshell.el
create mode 100644 testing/lisp/test-ob-eshell.el
diff --git a/doc/org-manual.org b/doc/org-manual.org
index 0ffa73911..38a6e8fa4 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -17579,6 +17579,7 @@ Code blocks in the following languages are supported.
| D | d | ditaa | ditaa |
| Graphviz | dot | Emacs Calc | calc |
| Emacs Lisp | emacs-lisp | Fortran | fortran |
+| Shell | sh | Eshell | eshell |
| Gnuplot | gnuplot | Haskell | haskell |
| Java | java | Javascript | js |
| LaTeX | latex | Ledger | ledger |
diff --git a/lisp/ob-eshell.el b/lisp/ob-eshell.el
new file mode 100644
index 000000000..25e52e9e8
--- /dev/null
+++ b/lisp/ob-eshell.el
@@ -0,0 +1,98 @@
+;;; 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.
+This function is called by `org-babel-execute-src-block'."
+ (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
+ (goto-char eshell-last-output-end)
+ (dolist (line (split-string full-body))
+ (insert line))
+ (eshell-send-input)
+ ;; get output of last input
+ (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)
+ (dolist (var-line var-lines)
+ (eshell-command var-line))
+ session))
+
+(defun ob-eshell-session-live-p (session)
+ "Detect Eshell SESSION exist."
+ (and (get-buffer session) t))
+
+(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)))))))
+
+(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
diff --git a/testing/lisp/test-ob-eshell.el b/testing/lisp/test-ob-eshell.el
new file mode 100644
index 000000000..77a750f0e
--- /dev/null
+++ b/testing/lisp/test-ob-eshell.el
@@ -0,0 +1,73 @@
+;;; test-ob-eshell.el
+
+;; Copyright (c) 2010-2014 Eric Schulte
+;; Authors: stardiviner
+
+;; This file is not part of GNU Emacs.
+
+;; This program 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.
+
+;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Comment:
+
+;; Template test file for Org tests
+
+;;; Code:
+(unless (featurep 'ob-eshell)
+ (signal 'missing-test-dependency "Support for Eshell code blocks"))
+
+(ert-deftest ob-eshell/execute ()
+ "Test ob-eshell execute."
+ (should
+ (string=
+ (org-test-with-temp-text
+ "#+begin_src eshell
+echo 2
+#+end_src"
+ (org-babel-execute-src-block))
+ ": 2")))
+
+(ert-deftest ob-eshell/variables-assignment ()
+ "Test ob-eshell variables assignment."
+ (should
+ (string=
+ (org-test-with-temp-text
+ "#+begin_src eshell :var hi=\"hello, world\"
+echo $hi
+#+end_src"
+ (org-babel-execute-src-block))
+ ": hello, world")))
+
+(ert-deftest ob-eshell/session ()
+ "Test ob-eshell session."
+ (should
+ (string=
+ (org-test-with-temp-text
+ "#+begin_src eshell :session
+(setq hi \"hello, world\")
+#+end_src
+
+#+begin_src eshell :session
+echo $hi
+#+end_src"
+ (org-babel-execute-src-block)
+ (org-babel-next-src-block)
+ (org-babel-execute-src-block)
+ (goto-char (org-babel-where-is-src-block-result))
+ (forward-line)
+ (buffer-substring-no-properties (point) (line-end-position)))
+ ": hello, world")))
+
+(provide 'test-ob-eshell)
+
+;;; test-ob-eshell.el ends here
--
2.17.0
[-- Attachment #1.3: Type: text/plain, Size: 202 bytes --]
--
[ stardiviner ] don't need to convince with trends.
Blog: https://stardiviner.github.io/
IRC(freenode): stardiviner
GPG: F09F650D7D674819892591401B5DF1C95AE89AC3
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]
next prev parent reply other threads:[~2018-04-22 2:01 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-22 2:00 [PATCH] Add support for Babel with Eshell, and some questions stardiviner
2018-04-22 2:01 ` stardiviner [this message]
2018-04-22 2:31 ` [PATCH] Add support for Babel with Eshell, (updated PATCH) stardiviner
2018-04-23 9:34 ` Nicolas Goaziou
2018-04-23 13:22 ` stardiviner
2018-04-23 21:35 ` Nicolas Goaziou
2018-04-24 2:39 ` stardiviner
2018-04-25 8:31 ` Nicolas Goaziou
2018-04-26 10:53 ` stardiviner
2018-05-02 14:53 ` stardiviner
2018-05-31 21:13 ` Nicolas Goaziou
2018-06-01 6:26 ` stardiviner
2018-06-02 10:16 ` Nicolas Goaziou
2018-06-04 2:11 ` stardiviner
2018-06-14 19:12 ` Nicolas Goaziou
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87bmecrod4.fsf@gmail.com \
--to=numbchild@gmail.com \
--cc=emacs-orgmode@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.