From mboxrd@z Thu Jan 1 00:00:00 1970 From: Frederick Giasson Subject: Re: [PATCH] New header parameter :show-process for Org-babel-clojure Date: Fri, 18 Nov 2016 12:02:16 -0500 Message-ID: <22d62a37-c714-ff60-0160-3fbda9771637@fgiasson.com> References: <036bf007-5e9c-310c-5bf8-0660c1b96a5c@fgiasson.com> <87lgwrgy7z.fsf@nicolasgoaziou.fr> <794c93aa-e2dc-2bfb-537e-0c89d8d34cea@fgiasson.com> <878tslp4ha.fsf@nicolasgoaziou.fr> <27089048-9dab-747f-51dc-20006717866e@fgiasson.com> <87inrlogee.fsf@nicolasgoaziou.fr> <87k2c0n82s.fsf@gmail.com> <87twb4n5z7.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------1A3178CD3C08FE6D24A0A9BE" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:33672) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c7mYR-0006m6-8u for emacs-orgmode@gnu.org; Fri, 18 Nov 2016 12:02:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c7mYM-0008K8-AZ for emacs-orgmode@gnu.org; Fri, 18 Nov 2016 12:02:23 -0500 Received: from s052.panelboxmanager.com ([72.55.186.33]:36460) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c7mYM-0008JS-3k for emacs-orgmode@gnu.org; Fri, 18 Nov 2016 12:02:18 -0500 Received: from mailnull by s052.panelboxmanager.com with sa-checked (Exim 4.87) (envelope-from ) id 1c7mYK-00432V-Ga for emacs-orgmode@gnu.org; Fri, 18 Nov 2016 12:02:17 -0500 Received: from modemcable199.69-130-66.mc.videotron.ca ([66.130.69.199]:56259 helo=[192.168.0.199]) by s052.panelboxmanager.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.87) (envelope-from ) id 1c7mYK-00432I-Az for emacs-orgmode@gnu.org; Fri, 18 Nov 2016 12:02:16 -0500 In-Reply-To: <87twb4n5z7.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: emacs-orgmode@gnu.org This is a multi-part message in MIME format. --------------1A3178CD3C08FE6D24A0A9BE Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Hi Nicolas, > Could you recreate the commit so it is self-contained? That should be it. Thanks, Fred --------------1A3178CD3C08FE6D24A0A9BE Content-Type: text/plain; charset=UTF-8; name="0001-Multiple-improvements-of-ob-clojure-have-been-perfor.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Multiple-improvements-of-ob-clojure-have-been-perfor.pa"; filename*1="tch" >From e19c358f9d4cab9b22c3e4e3bf6b8f6298e61b0e Mon Sep 17 00:00:00 2001 From: Frederick Giasson Date: Fri, 18 Nov 2016 12:01:02 -0500 Subject: [PATCH] Multiple improvements of ob-clojure have been performed in this commit. (1) better error handling. Errors and exceptions are output in the results section of a org-mode document. They were silenced with the previous version. (2) A new header parameter has been created: ":show-process" what will output the underlying NREPL processing in a new window + buffer (3) new setting "org-babel-clojure-sync-nrepl-timeout" to specify the Cider nrepl timeout. 'nil' means that it will never timeout Note that the behavior of ob-clojure *did not change* with these modications. Only the function (org-babel-execute:clojure) has been modified. --- etc/ORG-NEWS | 42 ++++++++++++++++++- lisp/ob-clojure.el | 117 ++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 134 insertions(+), 25 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index a360e35..5a5be53 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -23,8 +23,48 @@ into : (file (lambda () (sexp))) - ** New features +*** Babel +**** Org-babel-clojure, new setting 'org-babel-clojure-sync-nrepl-timeout' + +Creation of a new setting to specify the Cider timeout. By setting the =org-babel-clojure-sync-nrepl-timeout= +setting option. The value is in seconds and if set to =nil= then no timeout will occur. + +#+BEGIN_SRC emacs-lisp + ; Disable Cider's timeout + (setq org-babel-clojure-sync-nrepl-timeout nil) + + ; Set the timeout to 20 seconds + (setq org-babel-clojure-sync-nrepl-timeout 20) +#+END_SRC + + +**** Org-babel-clojure :show-process +A new block code header has been created for Org-babel-clojure that enables developers to output the process of an ongoing process into a new window/buffer. + +You can tell Org-babel-clojure to output the process of a running code block. + +To show that output you only have to specify the =:show-process= option in the code block's header like this: + +#+begin_example +#+BEGIN_SRC clojure :results output :show-process + (dotimes [n 10] + (println n ".") + (Thread/sleep 500)) +#+END_SRC +#+end_example + +If =:show-process= is specified that way, then when you will run the +code using =C-c C-c= a new window will open in Emacs. Everything that is +output by the REPL will immediately be added to that new window. + +When the processing of the code is finished, then the window and its buffer will be closed and the results will be reported in the +=#+RESULTS= section. + +Note that the =:results= parameter's behavior is *not* changed. If =silent= is specified, then no result will be displayed. If =output= is +specified then all the output from the window will appears in the +results section. If =value= is specified, then only the last returned +value of the code will be displayed in the results section. *** Horizontal rules are no longer ignored in LaTeX table math mode diff --git a/lisp/ob-clojure.el b/lisp/ob-clojure.el index 72ea77d..070e0ca 100644 --- a/lisp/ob-clojure.el +++ b/lisp/ob-clojure.el @@ -2,7 +2,7 @@ ;; Copyright (C) 2009-2016 Free Software Foundation, Inc. -;; Author: Joel Boehland, Eric Schulte, Oleh Krehel +;; Author: Joel Boehland, Eric Schulte, Oleh Krehel, Frederick Giasson ;; ;; Keywords: literate programming, reproducible research ;; Homepage: http://orgmode.org @@ -45,6 +45,9 @@ (declare-function cider-current-connection "ext:cider-client" (&optional type)) (declare-function cider-current-session "ext:cider-client" ()) (declare-function nrepl-dict-get "ext:nrepl-client" (dict key)) +(declare-function nrepl-dict-put "ext:nrepl-client" (dict key value)) +(declare-function nrepl--merge "ext:nrepl-client" (dict1 dict2)) +(declare-function nrepl-request:eval "ext:nrepl-client" (input callback connection)) (declare-function nrepl-sync-request:eval "ext:nrepl-client" (input connection session &optional ns)) (declare-function org-trim "org" (s &optional keep-lead)) @@ -56,6 +59,15 @@ (defvar org-babel-default-header-args:clojure '()) (defvar org-babel-header-args:clojure '((package . :any))) +(defcustom org-babel-clojure-sync-nrepl-timeout 10 + "Timeout value, in seconds, of a Clojure sync call. + If the value is nil, timeout is disabled." + :type 'integer + :version "25.1" + :package-version '(Org . "9.0") + :safe #'wholenump + :group 'org-babel) + (defcustom org-babel-clojure-backend (cond ((featurep 'cider) 'cider) (t 'slime)) @@ -84,34 +96,91 @@ body))) (defun org-babel-execute:clojure (body params) - "Execute a block of Clojure code with Babel." + "Execute a block of Clojure code with Babel. + The underlying process performed by the code block can be output + using the :show-process parameter." (let ((expanded (org-babel-expand-body:clojure body params)) - result) + (sbuffer "*Clojure Show Process Sub Buffer*") + (show (assq :show-process params)) + (response (list 'dict)) + status + result) (cl-case org-babel-clojure-backend (cider (require 'cider) (let ((result-params (cdr (assq :result-params params)))) - (setq result - (nrepl-dict-get - (nrepl-sync-request:eval - expanded (cider-current-connection) (cider-current-session)) - (if (or (member "output" result-params) - (member "pp" result-params)) - "out" - "value"))))) - (slime - (require 'slime) - (with-temp-buffer - (insert expanded) - (setq result - (slime-eval - `(swank:eval-and-grab-output - ,(buffer-substring-no-properties (point-min) (point-max))) - (cdr (assq :package params))))))) - (org-babel-result-cond (cdr (assq :result-params params)) - result - (condition-case nil (org-babel-script-escape result) - (error result))))) + ;; Check if the user want show the process in an output buffer/window. + (when show + ;; Create a new window with the show output buffer. + (switch-to-buffer-other-window sbuffer) + + ;; Run the Clojure code in nREPL. + (nrepl-request:eval + expanded + (lambda (resp) + (when (member "out" resp) + ;; Print the output of the nREPL in the output buffer. + (princ (nrepl-dict-get resp "out") (get-buffer sbuffer))) + (when (member "ex" resp) + ;; In case there is an exception, then add it to the output buffer as well. + (princ (nrepl-dict-get resp "ex") (get-buffer sbuffer)) + (princ (nrepl-dict-get resp "root-ex") (get-buffer sbuffer))) + (when (member "err" resp) + ;; In case there is an error, then add it to the output buffer as well. + (princ (nrepl-dict-get resp "err") (get-buffer sbuffer))) + (nrepl--merge response resp) + ;; Update the status of the nREPL output session. + (setq status (nrepl-dict-get response "status"))) + (cider-current-connection) + (cider-current-session)) + + ;; Wait until the nREPL code finished to be processed. + (while (not (member "done" status)) + (nrepl-dict-put response "status" (remove "need-input" status)) + (accept-process-output nil 0.01) + (redisplay)) + + ;; Delete the show buffer & window when the processing is finalized. + (mapc #'delete-window (get-buffer-window-list sbuffer nil t)) + (kill-buffer sbuffer) + + ;; Put the output or the value in the result section of the code block. + (setq result (concat (nrepl-dict-get response + (if (or (member "output" result-params) + (member "pp" result-params)) + "out" + "value")) + (nrepl-dict-get response "ex") + (nrepl-dict-get response "root-ex") + (nrepl-dict-get response "err")))) + ;; Check if user want to run code without showing the process. + (unless show + (setq response (let ((nrepl-sync-request-timeout + org-babel-clojure-sync-nrepl-timeout)) + (nrepl-sync-request:eval + expanded (cider-current-connection) (cider-current-session)))) + (setq result + (concat + (nrepl-dict-get response (if (or (member "output" result-params) + (member "pp" result-params)) + "out" + "value")) + (nrepl-dict-get response "ex") + (nrepl-dict-get response "root-ex") + (nrepl-dict-get response "err")))))) + (slime + (require 'slime) + (with-temp-buffer + (insert expanded) + (setq result + (slime-eval + `(swank:eval-and-grab-output + ,(buffer-substring-no-properties (point-min) (point-max))) + (cdr (assq :package params))))))) + (org-babel-result-cond (cdr (assq :result-params params)) + result + (condition-case nil (org-babel-script-escape result) + (error result))))) (provide 'ob-clojure) -- 1.9.5.msysgit.0 --------------1A3178CD3C08FE6D24A0A9BE--