ob-clojure: Small refactoring

* lisp/ob-clojure.el (org-babel-execute:clojure): Small refactoring.
This commit is contained in:
Nicolas Goaziou 2016-11-19 07:35:23 +01:00
parent 56c0ce4a8d
commit cd4186c5c3
1 changed files with 66 additions and 67 deletions

View File

@ -103,83 +103,82 @@ If the value is nil, timeout is disabled."
The underlying process performed by the code block can be output The underlying process performed by the code block can be output
using the :show-process parameter." using the :show-process parameter."
(let ((expanded (org-babel-expand-body:clojure body params)) (let ((expanded (org-babel-expand-body:clojure body params))
(sbuffer "*Clojure Show Process Sub Buffer*")
(show (assq :show-process params)) (show (assq :show-process params))
(response (list 'dict)) (response (list 'dict))
status
result) result)
(cl-case org-babel-clojure-backend (cl-case org-babel-clojure-backend
(cider (cider
(require 'cider) (require 'cider)
(let ((result-params (cdr (assq :result-params params)))) (let ((result-params (cdr (assq :result-params params))))
;; Check if the user wants to show the process in an output (if (not show)
;; buffer/window. ;; Run code without showing the process.
(when show (progn
;; Create a new window with the show output buffer. (setq response
(switch-to-buffer-other-window sbuffer) (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"))))
;; Show the process in an output buffer/window.
(let ((process-buffer (switch-to-buffer-other-window
"*Clojure Show Process Sub Buffer*"))
status)
;; 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") process-buffer))
(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") process-buffer)
(princ (nrepl-dict-get resp "root-ex") process-buffer))
(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") process-buffer))
(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))
;; Run the Clojure code in nREPL. ;; Wait until the nREPL code finished to be processed.
(nrepl-request:eval (while (not (member "done" status))
expanded (nrepl-dict-put response "status" (remove "need-input" status))
(lambda (resp) (accept-process-output nil 0.01)
(when (member "out" resp) (redisplay))
;; 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. ;; Delete the show buffer & window when the processing is
(while (not (member "done" status)) ;; finalized.
(nrepl-dict-put response "status" (remove "need-input" status)) (mapc #'delete-window
(accept-process-output nil 0.01) (get-buffer-window-list process-buffer nil t))
(redisplay)) (kill-buffer process-buffer)
;; Delete the show buffer & window when the processing is ;; Put the output or the value in the result section of
;; finalized. ;; the code block.
(mapc #'delete-window (get-buffer-window-list sbuffer nil t)) (setq result
(kill-buffer sbuffer) (concat
(nrepl-dict-get response
;; Put the output or the value in the result section of the (if (or (member "output" result-params)
;; code block. (member "pp" result-params))
(setq result "out"
(concat (nrepl-dict-get response "value"))
(if (or (member "output" result-params) (nrepl-dict-get response "ex")
(member "pp" result-params)) (nrepl-dict-get response "root-ex")
"out" (nrepl-dict-get response "err")))))))
"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 (slime
(require 'slime) (require 'slime)
(with-temp-buffer (with-temp-buffer