emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: stardiviner <numbchild@gmail.com>
To: Nicolas Goaziou <mail@nicolasgoaziou.fr>
Cc: org-mode <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] add :session support for ob-js.el
Date: Sun, 25 Mar 2018 11:31:33 +0800	[thread overview]
Message-ID: <366d0e08-8e60-88a2-f7e5-28db5ba3deb8@gmail.com> (raw)
In-Reply-To: <87a7v7dkdp.fsf@nicolasgoaziou.fr>

[-- Attachment #1: Type: text/plain, Size: 1315 bytes --]

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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-js.el-support-session-for-skewer-mode-REPL.patch --]
[-- Type: text/x-patch; name="0001-ob-js.el-support-session-for-skewer-mode-REPL.patch", Size: 5480 bytes --]

From 140ef4c23fb3e13a772ca01b92b14bccc47043fb Mon Sep 17 00:00:00 2001
From: stardiviner <numbchild@gmail.com>
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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-ob-js.el-support-session-for-Indium-Node-REPL.patch --]
[-- Type: text/x-patch; name="0002-ob-js.el-support-session-for-Indium-Node-REPL.patch", Size: 2121 bytes --]

From e3d04ef080ef21d5741176ca7b02c25e35b8d414 Mon Sep 17 00:00:00 2001
From: stardiviner <numbchild@gmail.com>
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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0003-ob-js.el-support-session-for-js-comint-REPL.patch --]
[-- Type: text/x-patch; name="0003-ob-js.el-support-session-for-js-comint-REPL.patch", Size: 1726 bytes --]

From a743e2252aa7030007d1f0f57f26640ffbbf14a1 Mon Sep 17 00:00:00 2001
From: stardiviner <numbchild@gmail.com>
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


[-- Attachment #5: 0004-ob-js.el-org-babel-prep-session-js-replace-mapc-with.patch --]
[-- Type: text/x-patch, Size: 1093 bytes --]

From 7fe493e915750b8337ad8ce830f595ab3648bc65 Mon Sep 17 00:00:00 2001
From: stardiviner <numbchild@gmail.com>
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


  parent reply	other threads:[~2018-03-25  3:31 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-12  9:02 [PATCH] add :session support for ob-js.el stardiviner
2018-03-13  8:43 ` Nicolas Goaziou
2018-03-13 11:51   ` stardiviner
2018-03-17 11:14     ` Nicolas Goaziou
2018-03-17 17:51       ` About the assignment of FSF papers stardiviner
2018-03-18 16:15         ` Nicolas Goaziou
2018-03-22 18:47           ` stardiviner
2018-03-24 19:02             ` Nicolas Goaziou
2018-03-25  3:31       ` stardiviner [this message]
2018-03-25 21:09         ` [PATCH] add :session support for ob-js.el Nicolas Goaziou
2018-03-26  1:40           ` stardiviner

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

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=366d0e08-8e60-88a2-f7e5-28db5ba3deb8@gmail.com \
    --to=numbchild@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    /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 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).