From mboxrd@z Thu Jan 1 00:00:00 1970 From: stardiviner Subject: Re: [PATCH] Add support for Babel with Eshell, (updated PATCH) Date: Thu, 26 Apr 2018 18:53:43 +0800 Message-ID: <87sh7i2q88.fsf@gmail.com> References: <87d0ysroer.fsf@gmail.com> <87bmecrod4.fsf@gmail.com> <87a7twrmyv.fsf@gmail.com> <87po2q5kqk.fsf@nicolasgoaziou.fr> <87h8o26orj.fsf@gmail.com> <87sh7lobc5.fsf@nicolasgoaziou.fr> <87d0yptjjm.fsf@gmail.com> <87h8nzlm9x.fsf@nicolasgoaziou.fr> Reply-To: numbchild@gmail.com Mime-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:41559) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fBeXK-00085D-Do for emacs-orgmode@gnu.org; Thu, 26 Apr 2018 06:54:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fBeXG-0004Uv-Hq for emacs-orgmode@gnu.org; Thu, 26 Apr 2018 06:54:02 -0400 Received: from [183.249.129.92] (port=5067 helo=dark.localdomain) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fBeXF-0004Pz-Oc for emacs-orgmode@gnu.org; Thu, 26 Apr 2018 06:53:58 -0400 In-reply-to: <87h8nzlm9x.fsf@nicolasgoaziou.fr> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Nicolas Goaziou Cc: org-mode --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain Nicolas Goaziou writes: > I'm not convinced this is less work to edit, but I changed it anyway. It > makes it easier to follow alphabetical order. I also wrapped > identifiers within verbatim markup. > You're right, still not improved a lot. Anyway, thanks. And I updated patch inclued in attachment. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-ob-eshell.el-Add-Eshell-support-for-Babel.patch Content-Transfer-Encoding: quoted-printable From=20bea5c4454ee53307578f4c6e10e2b8e06f9bb70b Mon Sep 17 00:00:00 2001 From: stardiviner 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 Babe= l. (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 Babel language eshell identity. =2D-- doc/org-manual.org | 27 +++++---- lisp/ob-eshell.el | 105 +++++++++++++++++++++++++++++++++ testing/lisp/test-ob-eshell.el | 73 +++++++++++++++++++++++ 3 files changed, 192 insertions(+), 13 deletions(-) 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 bdef9f61b..de62d15d2 100644 =2D-- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -17573,8 +17573,8 @@ code block header arguments: =20 Code blocks in the following languages are supported. =20 =2D| Language | Identifier | Language | Identifier | =2D|------------+--------------+----------------+--------------| +| Language | Identifier | Language | Identifier | +|------------+------------+----------------+------------| | Asymptote | =3Dasymptote=3D | Lua | =3Dlua=3D | | Awk | =3Dawk=3D | MATLAB | =3Dmatlab=3D | | C | =3DC=3D | Mscgen | =3Dmscgen=3D | @@ -17585,17 +17585,18 @@ Code blocks in the following languages are suppor= ted. | ditaa | =3Dditaa=3D | Perl | =3Dperl=3D | | Emacs Calc | =3Dcalc=3D | Plantuml | =3Dplantuml=3D | | Emacs Lisp | =3Demacs-lisp=3D | Processing.js | =3Dprocessing=3D | =2D| Fortran | =3Dfortran=3D | Python | =3Dpython=3D | =2D| Gnuplot | =3Dgnuplot=3D | R | =3DR=3D | =2D| GNU Screen | =3Dscreen=3D | Ruby | =3Druby=3D | =2D| Graphviz | =3Ddot=3D | Sass | =3Dsass=3D | =2D| Haskell | =3Dhaskell=3D | Scheme | =3Dscheme=3D | =2D| Java | =3Djava=3D | Sed | =3Dsed=3D | =2D| Javascript | =3Djs=3D | shell | =3Dsh=3D | =2D| LaTeX | =3Dlatex=3D | SQL | =3Dsql=3D | =2D| Ledger | =3Dledger=3D | SQLite | =3Dsqlite=3D | =2D| Lilypond | =3Dlilypond=3D | Vala | =3Dvala=3D | =2D| Lisp | =3Dlisp=3D | | | +| Eshell | =3Deshell=3D | Python | =3Dpython=3D | +| Fortran | =3Dfortran=3D | R | =3DR=3D | +| Gnuplot | =3Dgnuplot=3D | Ruby | =3Druby=3D | +| GNU Screen | =3Dscreen=3D | Sass | =3Dsass=3D | +| Graphviz | =3Ddot=3D | Scheme | =3Dscheme=3D | +| Haskell | =3Dhaskell=3D | Sed | =3Dsed=3D | +| Java | =3Djava=3D | shell | =3Dsh=3D | +| Javascript | =3Djs=3D | SQL | =3Dsql=3D | +| LaTeX | =3Dlatex=3D | SQLite | =3Dsqlite=3D | +| Ledger | =3Dledger=3D | Vala | =3Dvala=3D | +| Lilypond | =3Dlilypond=3D | | | +| Lisp | =3Dlisp=3D | | | =20 Additional documentation for some languages is at https://orgmode.org/worg/org-contrib/babel/languages.html. diff --git a/lisp/ob-eshell.el b/lisp/ob-eshell.el new file mode 100644 index 000000000..f1cd859a4 =2D-- /dev/null +++ b/lisp/ob-eshell.el @@ -0,0 +1,105 @@ +;;; ob-eshell.el --- Babel Functions for Eshell -*- lexical-bin= ding: t; -*- + +;; Copyright (C) 2009-2018 Free Software Foundation, Inc. + +;; Author: stardiviner +;; 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 . + +;;; 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=3D 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 assign= ments." + (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..21e7484cd =2D-- /dev/null +++ b/testing/lisp/test-ob-eshell.el @@ -0,0 +1,73 @@ +;;; test-ob-eshell.el + +;; Copyright (c) 2018-2022 stardiviner +;; 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 . + +;;; 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=3D + (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=3D + (org-test-with-temp-text + "#+begin_src eshell :var hi=3D\"hello, world\" +echo $hi +#+end_src" + (org-babel-execute-src-block)) + ": hello, world"))) + +(ert-deftest ob-eshell/session () + "Test ob-eshell session." + (should + (string=3D + (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 =2D-=20 2.17.0 --=-=-= Content-Type: text/plain -- [ stardiviner ] don't need to convince with trends. Blog: https://stardiviner.github.io/ IRC(freenode): stardiviner GPG: F09F650D7D674819892591401B5DF1C95AE89AC3 --=-=-=-- --==-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEE8J9lDX1nSBmJJZFAG13xyVromsMFAlrhr7kACgkQG13xyVro msPV9wf9EqqRh8EpO8QuIVX2PBDc40GLO+PCSUrsqYEJryqQBpXFql6CHVP0MLDD zvmoBJ5/wYQBL4gYIpQFPM3QQfd6xTAGITP27TL0TJ13c6e7NMRthiPXV5kychMP mK0hwEdueEUPX0bEBBNXn2t9vtkSGhgpXBH2b5qtzTiBvn4vH7JoLCm5a65vVRhi yJQA129p+PT0TqKia8Q+JO39O6Gi5o+q6Tu27ZTLcy6mZqgQvJEYGETH43xAWTpV 8KjlUtumzyqbzAYwzNvGZ6yss9EhvNJsIR35hsU3W9D7YniYXhPdtbH5lwAIgaPB LKZP1ITxF8tHidUdoxyY0MGCgnv8sA== =XqED -----END PGP SIGNATURE----- --==-=-=--