Forráskód Böngészése

ob-clojure: Add :show-process

* lisp/ob-clojure.el (org-babel-clojure-sync-nrepl-timeout): New variable.
(org-babel-execute:clojure): Implement :show-process.  Better error
handling.
Frederick Giasson 8 éve
szülő
commit
56c0ce4a8d
2 módosított fájl, 131 hozzáadás és 12 törlés
  1. 39 1
      etc/ORG-NEWS
  2. 92 11
      lisp/ob-clojure.el

+ 39 - 1
etc/ORG-NEWS

@@ -23,8 +23,46 @@ into
 
 : (file (lambda () (sexp)))
 
-
 ** New features
+*** 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.
+
+**** Clojure: new header ~:show-process~
+
+A new block code header has been created for Org Babel that enables
+developers to output the process of an ongoing process into a new
+window/buffer.
+
+You can tell Org Babel 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
 

+ 92 - 11
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
@@ -44,18 +44,33 @@
 
 (declare-function cider-current-connection "ext:cider-client" (&optional type))
 (declare-function cider-current-session "ext:cider-client" ())
+(declare-function nrepl--merge "ext:nrepl-client" (dict1 dict2))
 (declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
+(declare-function nrepl-dict-put "ext:nrepl-client" (dict key value))
+(declare-function nrepl-request:eval "ext:nrepl-client"
+		  (input callback connection &optional session ns line column additional-params))
 (declare-function nrepl-sync-request:eval "ext:nrepl-client"
 		  (input connection session &optional ns))
 (declare-function org-trim "org" (s &optional keep-lead))
 (declare-function slime-eval "ext:slime" (sexp &optional package))
 
+(defvar nrepl-sync-request-timeout)
+
 (defvar org-babel-tangle-lang-exts)
 (add-to-list 'org-babel-tangle-lang-exts '("clojure" . "clj"))
 
 (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."
+  :group 'org-babel
+  :type 'integer
+  :version "25.2"
+  :package-version '(Org . "9.1")
+  :safe #'wholenump)
+
 (defcustom org-babel-clojure-backend
   (cond ((featurep 'cider) 'cider)
 	(t 'slime))
@@ -84,21 +99,87 @@
       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")))))
+         ;; Check if the user wants to 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