From: Frederick Giasson <fred@fgiasson.com>
To: emacs-orgmode@gnu.org
Subject: Re: [PATCH] New header parameter :show-process for Org-babel-clojure
Date: Mon, 14 Nov 2016 15:46:52 -0500 [thread overview]
Message-ID: <794c93aa-e2dc-2bfb-537e-0c89d8d34cea@fgiasson.com> (raw)
In-Reply-To: <87lgwrgy7z.fsf@nicolasgoaziou.fr>
[-- Attachment #1: Type: text/plain, Size: 306 bytes --]
Hi Nicolas,
> Thank you. Some nit-picks follow.
I made all the changes requested and merged in a single commit. See the
attachment.
> BTW, did you sign FSF papers already? I cannot find any reference in
> org-contribute.org?
I did as of today. Waiting to get a reply from them on that.
Thanks,
Fred
[-- Attachment #2: 0001-Multiple-improvements-of-ob-clojure-have-been-perfor.patch --]
[-- Type: text/plain, Size: 7921 bytes --]
From 3eb282c554f8099627b748e7c4eb07d0e810a2ae Mon Sep 17 00:00:00 2001
From: Frederick Giasson <fred@fgiasson.com>
Date: Mon, 14 Nov 2016 15:43:16 -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
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 | 32 ++++++++++++++++
lisp/ob-clojure.el | 105 +++++++++++++++++++++++++++++++++++++++++------------
2 files changed, 113 insertions(+), 24 deletions(-)
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index a360e35..95c97a7 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -27,6 +27,38 @@ into
** New features
*** Horizontal rules are no longer ignored in LaTeX table math mode
+*** 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.
+
* Version 9.0
diff --git a/lisp/ob-clojure.el b/lisp/ob-clojure.el
index 72ea77d..f78dd69 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
@@ -84,34 +84,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
next prev parent reply other threads:[~2016-11-14 20:47 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-11-07 19:19 [PATCH] New header parameter :show-process for Org-babel-clojure Frederick Giasson
2016-11-10 15:18 ` Nicolas Goaziou
2016-11-10 17:20 ` Frederick Giasson
2016-11-10 23:55 ` Nicolas Goaziou
2016-11-14 20:46 ` Frederick Giasson [this message]
2016-11-15 7:49 ` Nicolas Goaziou
2016-11-17 13:39 ` Frederick Giasson
2016-11-17 23:06 ` Nicolas Goaziou
2016-11-18 15:03 ` Aaron Ecay
2016-11-18 15:09 ` Frederick Giasson
2016-11-18 15:49 ` Nicolas Goaziou
2016-11-18 17:02 ` Frederick Giasson
2016-11-19 7:40 ` Nicolas Goaziou
2016-11-18 15:31 ` Frederick Giasson
2016-11-18 15:12 ` Frederick Giasson
2016-11-18 15:51 ` Nicolas Goaziou
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=794c93aa-e2dc-2bfb-537e-0c89d8d34cea@fgiasson.com \
--to=fred@fgiasson.com \
--cc=emacs-orgmode@gnu.org \
/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).