From mboxrd@z Thu Jan 1 00:00:00 1970 From: stardiviner Subject: Re: [PATCH] add :session support for ob-js.el Date: Sun, 25 Mar 2018 11:31:33 +0800 Message-ID: <366d0e08-8e60-88a2-f7e5-28db5ba3deb8@gmail.com> References: <4f31b760-06ac-5c32-8dbd-76f1017d3cd1@gmail.com> <87efko8iwr.fsf@nicolasgoaziou.fr> <307cac72-36e9-0ba6-262b-a12f291953a0@gmail.com> <87a7v7dkdp.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------C7676866AEE3B8E3458B5900" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:44077) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ezwNk-0003J2-FZ for emacs-orgmode@gnu.org; Sat, 24 Mar 2018 23:31:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ezwNg-0003fF-9O for emacs-orgmode@gnu.org; Sat, 24 Mar 2018 23:31:44 -0400 Received: from mail-pf0-x234.google.com ([2607:f8b0:400e:c00::234]:44767) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ezwNf-0003dV-SG for emacs-orgmode@gnu.org; Sat, 24 Mar 2018 23:31:40 -0400 Received: by mail-pf0-x234.google.com with SMTP id m68so6211179pfm.11 for ; Sat, 24 Mar 2018 20:31:39 -0700 (PDT) In-Reply-To: <87a7v7dkdp.fsf@nicolasgoaziou.fr> Content-Language: en-US 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 This is a multi-part message in MIME format. --------------C7676866AEE3B8E3458B5900 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit I attached my new generated patches. On 03/17/2018 07:14 PM, Nicolas Goaziou wrote: > Some comments follow. >> + (result (if (not (string= (cdr (assq :session params)) "none")) > You can integrate the test above in the `cond': > > (cond > ((string= "none" (cdr (assq :session params))) nil) > ((string= "*JS REPL*" session) ...) > ...) This is modified in new patch. >> + (sit-for .5) (goto-char (point-max)) > (sit-for .5) > (goto-char (point-max)) This is in original code, might because ob-js initialize REPL backend need some time so original author added `site-for`. After test, this seems does not matter. So remote the sit-for now. > >> + (mapc (lambda (var) >> + (insert var) (comint-send-input nil t) >> + (org-babel-comint-wait-for-output session) >> + (sit-for .1) (goto-char (point-max))) var-lines))) >> session)) > (dolist (var ...) > ...) modified in new patch. I don't know how to apply master branch new update on my local `ob-js-session` branch so to get ob-js related commits on top to generate new patches. (Do you have any idea on this?) The new patches are behind latest `master` branch. But should be compatible still. If generating patches based on latest `master` branch commit is must, I can do that. --------------C7676866AEE3B8E3458B5900 Content-Type: text/x-patch; name="0001-ob-js.el-support-session-for-skewer-mode-REPL.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="0001-ob-js.el-support-session-for-skewer-mode-REPL.patch" >From 140ef4c23fb3e13a772ca01b92b14bccc47043fb Mon Sep 17 00:00:00 2001 From: stardiviner Date: Thu, 8 Mar 2018 17:15:58 +0800 Subject: [PATCH 1/4] * ob-js.el: support :session for skewer-mode REPL. --- etc/ORG-NEWS | 4 +++ lisp/ob-js.el | 81 ++++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 54 insertions(+), 31 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index bd9d2a8f7..11ecaa5d3 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -113,6 +113,10 @@ now sort according to the locale’s collation rules instead of by code-point. ** New features +*** Add ~:session~ support of ob-js for skewer-mode +#+begin_src js :session "*skewer-repl*" +console.log("stardiviner") +#+end_src *** Add support for links to LaTeX equations in HTML export Use MathJax links when enabled (by ~org-html-with-latex~), otherwise add a label to the rendered equation. diff --git a/lisp/ob-js.el b/lisp/ob-js.el index 38c8c39ac..920d104f8 100644 --- a/lisp/ob-js.el +++ b/lisp/ob-js.el @@ -41,6 +41,8 @@ (require 'ob) (declare-function run-mozilla "ext:moz" (arg)) +(declare-function httpd-start "simple-httpd" ()) +(declare-function run-skewer "skewer-mode" ()) (defvar org-babel-default-header-args:js '() "Default header arguments for js code blocks.") @@ -52,7 +54,10 @@ "Name of command used to evaluate js blocks." :group 'org-babel :version "24.1" - :type 'string) + :type '(choice (const "node") + (const "mozrepl") + (const "skewer-mode")) + :safe #'stringp) (defvar org-babel-js-function-wrapper "require('sys').print(require('sys').inspect(function(){\n%s\n}()));" @@ -62,22 +67,13 @@ "Execute a block of Javascript code with org-babel. This function is called by `org-babel-execute-src-block'" (let* ((org-babel-js-cmd (or (cdr (assq :cmd params)) org-babel-js-cmd)) + (session (cdr (assq :session params))) (result-type (cdr (assq :result-type params))) (full-body (org-babel-expand-body:generic body params (org-babel-variable-assignments:js params))) - (result (if (not (string= (cdr (assq :session params)) "none")) - ;; session evaluation - (let ((session (org-babel-prep-session:js - (cdr (assq :session params)) params))) - (nth 1 - (org-babel-comint-with-output - (session (format "%S" org-babel-js-eoe) t body) - (mapc - (lambda (line) - (insert (org-babel-chomp line)) - (comint-send-input nil t)) - (list body (format "%S" org-babel-js-eoe)))))) - ;; external evaluation + (result (cond + ;; no session specified, external evaluation + ((string= session "none") (let ((script-file (org-babel-temp-file "js-script-"))) (with-temp-file script-file (insert @@ -87,7 +83,19 @@ This function is called by `org-babel-execute-src-block'" full-body))) (org-babel-eval (format "%s %s" org-babel-js-cmd - (org-babel-process-file-name script-file)) ""))))) + (org-babel-process-file-name script-file)) ""))) + ;; session evaluation + (t + (let ((session (org-babel-prep-session:js + (cdr (assq :session params)) params))) + (nth 1 + (org-babel-comint-with-output + (session (format "%S" org-babel-js-eoe) t body) + (dolist (code (list body (format "%S" org-babel-js-eoe))) + (lambda (code) + (insert (org-babel-chomp code)) + (comint-send-input nil t))))))) + ))) (org-babel-result-cond (cdr (assq :result-params params)) result (org-babel-js-read result)))) @@ -140,22 +148,33 @@ specifying a variable of the same value." (defun org-babel-js-initiate-session (&optional session) "If there is not a current inferior-process-buffer in SESSION then create. Return the initialized session." - (unless (string= session "none") - (cond - ((string= "mozrepl" org-babel-js-cmd) - (require 'moz) - (let ((session-buffer (save-window-excursion - (run-mozilla nil) - (rename-buffer session) - (current-buffer)))) - (if (org-babel-comint-buffer-livep session-buffer) - (progn (sit-for .25) session-buffer) - (sit-for .5) - (org-babel-js-initiate-session session)))) - ((string= "node" org-babel-js-cmd ) - (error "Session evaluation with node.js is not supported")) - (t - (error "Sessions are only supported with mozrepl add \":cmd mozrepl\""))))) + (cond + ((string= session "none") + (warn "Session evaluation of ob-js is not supported")) + ((string= "*skewer-repl*" session) + (require 'skewer-repl) + (let ((session-buffer (get-buffer "*skewer-repl*"))) + (if (and (org-babel-comint-buffer-livep (get-buffer session-buffer)) + (comint-check-proc session-buffer)) + session-buffer + ;; start skewer REPL. + (httpd-start) + (run-skewer) + session-buffer))) + ((string= "mozrepl" org-babel-js-cmd) + (require 'moz) + (let ((session-buffer (save-window-excursion + (run-mozilla nil) + (rename-buffer session) + (current-buffer)))) + (if (org-babel-comint-buffer-livep session-buffer) + (progn (sit-for .25) session-buffer) + (sit-for .5) + (org-babel-js-initiate-session session)))) + ((string= "node" org-babel-js-cmd ) + (error "Session evaluation with node.js is not supported")) + (t + (error "Sessions are only supported with mozrepl add \":cmd mozrepl\"")))) (provide 'ob-js) -- 2.16.2 --------------C7676866AEE3B8E3458B5900 Content-Type: text/x-patch; name="0002-ob-js.el-support-session-for-Indium-Node-REPL.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="0002-ob-js.el-support-session-for-Indium-Node-REPL.patch" >From e3d04ef080ef21d5741176ca7b02c25e35b8d414 Mon Sep 17 00:00:00 2001 From: stardiviner Date: Sun, 18 Mar 2018 01:19:29 +0800 Subject: [PATCH 2/4] * ob-js.el: support :session for Indium Node REPL. --- etc/ORG-NEWS | 4 ++++ lisp/ob-js.el | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 11ecaa5d3..0e0b6fa33 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -113,6 +113,10 @@ now sort according to the locale’s collation rules instead of by code-point. ** New features +*** Add ~:session~ support of ob-js for Indium +#+begin_src js :session "*JS REPL*" +console.log("stardiviner") +#+end_src *** Add ~:session~ support of ob-js for skewer-mode #+begin_src js :session "*skewer-repl*" console.log("stardiviner") diff --git a/lisp/ob-js.el b/lisp/ob-js.el index 920d104f8..06df20bf7 100644 --- a/lisp/ob-js.el +++ b/lisp/ob-js.el @@ -43,6 +43,8 @@ (declare-function run-mozilla "ext:moz" (arg)) (declare-function httpd-start "simple-httpd" ()) (declare-function run-skewer "skewer-mode" ()) +(declare-function indium-run-node "indium-nodejs" (command)) +(declare-function indium-eval "indium-interaction" (string &optional callback)) (defvar org-babel-default-header-args:js '() "Default header arguments for js code blocks.") @@ -56,7 +58,8 @@ :version "24.1" :type '(choice (const "node") (const "mozrepl") - (const "skewer-mode")) + (const "skewer-mode") + (const "indium")) :safe #'stringp) (defvar org-babel-js-function-wrapper @@ -84,6 +87,13 @@ This function is called by `org-babel-execute-src-block'" (org-babel-eval (format "%s %s" org-babel-js-cmd (org-babel-process-file-name script-file)) ""))) + ;; Indium Node REPL + ;; separate case because Indium REPL is not inherited from comint-mode + ((string= session "*JS REPL*") + (require 'indium-repl) + (unless (get-buffer session) + (indium-run-node)) + (indium-eval full-body)) ;; session evaluation (t (let ((session (org-babel-prep-session:js -- 2.16.2 --------------C7676866AEE3B8E3458B5900 Content-Type: text/x-patch; name="0003-ob-js.el-support-session-for-js-comint-REPL.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="0003-ob-js.el-support-session-for-js-comint-REPL.patch" >From a743e2252aa7030007d1f0f57f26640ffbbf14a1 Mon Sep 17 00:00:00 2001 From: stardiviner Date: Sun, 18 Mar 2018 01:33:12 +0800 Subject: [PATCH 3/4] * ob-js.el: support :session for js-comint REPL. --- etc/ORG-NEWS | 4 ++++ lisp/ob-js.el | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 0e0b6fa33..71895e6a0 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -113,6 +113,10 @@ now sort according to the locale’s collation rules instead of by code-point. ** New features +*** Add ~:session~ support of ob-js for js-comint +#+begin_src js :session "*Javascript REPL*" +console.log("stardiviner") +#+end_src *** Add ~:session~ support of ob-js for Indium #+begin_src js :session "*JS REPL*" console.log("stardiviner") diff --git a/lisp/ob-js.el b/lisp/ob-js.el index 06df20bf7..c829c4c74 100644 --- a/lisp/ob-js.el +++ b/lisp/ob-js.el @@ -59,7 +59,8 @@ :type '(choice (const "node") (const "mozrepl") (const "skewer-mode") - (const "indium")) + (const "indium") + (const "js-comint")) :safe #'stringp) (defvar org-babel-js-function-wrapper @@ -171,6 +172,15 @@ then create. Return the initialized session." (httpd-start) (run-skewer) session-buffer))) + ((string= "*Javascript REPL*" session) + (require 'js-comint) + (let ((session-buffer "*Javascript REPL*")) + (if (and (org-babel-comint-buffer-livep (get-buffer session-buffer)) + (comint-check-proc session-buffer)) + session-buffer + (call-interactively 'run-js) + (sit-for .5) + session-buffer))) ((string= "mozrepl" org-babel-js-cmd) (require 'moz) (let ((session-buffer (save-window-excursion -- 2.16.2 --------------C7676866AEE3B8E3458B5900 Content-Type: text/x-patch; name="0004-ob-js.el-org-babel-prep-session-js-replace-mapc-with.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0004-ob-js.el-org-babel-prep-session-js-replace-mapc-with.pa"; filename*1="tch" >From 7fe493e915750b8337ad8ce830f595ab3648bc65 Mon Sep 17 00:00:00 2001 From: stardiviner Date: Sun, 25 Mar 2018 11:26:37 +0800 Subject: [PATCH 4/4] * ob-js.el (org-babel-prep-session:js) replace mapc with dolist. --- lisp/ob-js.el | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lisp/ob-js.el b/lisp/ob-js.el index c829c4c74..9c42687c0 100644 --- a/lisp/ob-js.el +++ b/lisp/ob-js.el @@ -142,11 +142,11 @@ specifying a variable of the same value." (var-lines (org-babel-variable-assignments:js params))) (when session (org-babel-comint-in-buffer session - (sit-for .5) (goto-char (point-max)) - (mapc (lambda (var) - (insert var) (comint-send-input nil t) - (org-babel-comint-wait-for-output session) - (sit-for .1) (goto-char (point-max))) var-lines))) + (goto-char (point-max)) + (dolist (var var-lines) + (insert var) (comint-send-input nil t) + (org-babel-comint-wait-for-output session) + (sit-for .1) (goto-char (point-max))))) session)) (defun org-babel-variable-assignments:js (params) -- 2.16.2 --------------C7676866AEE3B8E3458B5900--