From mboxrd@z Thu Jan 1 00:00:00 1970 From: "numbchild@gmail.com" Subject: How to pass `format` result string to org-babel-execute:clojure function? Date: Thu, 15 Feb 2018 14:10:48 +0800 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="001a11c17f449cb24805653a18c3" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:48329) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emClO-0008FZ-4A for emacs-orgmode@gnu.org; Thu, 15 Feb 2018 01:11:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1emClM-00020r-6J for emacs-orgmode@gnu.org; Thu, 15 Feb 2018 01:11:22 -0500 Received: from mail-oi0-x22f.google.com ([2607:f8b0:4003:c06::22f]:45574) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1emClL-00020k-VO for emacs-orgmode@gnu.org; Thu, 15 Feb 2018 01:11:20 -0500 Received: by mail-oi0-x22f.google.com with SMTP id 23so6227179oip.12 for ; Wed, 14 Feb 2018 22:11:19 -0800 (PST) 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: Org-mode --001a11c17f449cb24805653a18c3 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I try add advice as I posted at here https://emacs.stackexchange.com/questions/38818/how-to-write-this-advice-fo= r-inject-code-into-let-binding And here is my final code (some lines commented out for easy debug): ```elisp ;;; Support header arguments :results file :file "image.png" (defun ob-clojure-literate-inject-code (args) "Inject Clojure code into `BODY' in `ARGS'. This can be used as :filter-args advice of `org-babel-expand-body:clojure'. It is used to change Clojure currently working directory and generate graphics image file result." (let* ((body (nth 0 args)) (params (nth 1 args)) (dir (cdr (assq :dir params))) (default-directory (and (buffer-file-name) (file-name-directory (buffer-file-name)))) (directory (and dir (file-name-as-directory (expand-file-name dir)))) (result-type (cdr (assq :results params))) (out-file (cdr (assq :file params))) (add-to-body (lambda (code) (setq body (concat code "\n" body)))) (append-to-body (lambda (code) (setq body (concat body "\n" code)))) ) (unless (file-directory-p (expand-file-name directory)) (warn (format "ob-clojure-literate directory %s does not exist, please create it." dir))) ;; (when directory ;; (funcall add-to-body (format "(System/setProperty \"user.dir\" \"%s\")" directory))) (when (string-match-p (regexp-opt '("graphics" "file")) result-type) ;; (member "graphics" (cdr (assq :result-params params))) ;; (funcall add-to-body "(import 'java.io.FileOutputStream)") ;; use static global variable `ob-clojure-literate-incanter-plot' as convention. ;; (funcall append-to-body ;; (format "(def ob-clojure-literate-incanter-plot-file (FileOutputStream. \"%s\"))" ;; (concat directory out-file))) ;; (funcall append-to-body ;; "(save ob-clojure-literate-incanter-plot ob-clojure-literate-incanter-plot-file)") ) (when out-file ;; (funcall append-to-body ;; (format "(view ob-clojure-literate-incanter-plot)")) (funcall append-to-body (format "(save ob-clojure-literate-incanter-plot \"%s\")" (concat directory out-file))) ) (list body params) ; return modified argument list )) (advice-add 'org-babel-expand-body:clojure :filter-args #'ob-clojure-literate-inject-code) ``` When I add upper advice code. And execute the following org-babel src block= : ```org #+begin_src clojure :cache no :dir "data/images" :results graphics :file "ob-clojure-literate.png" (use '(incanter core stats datasets charts io pdf)) (def ob-clojure-literate-incanter-plot (histogram (sample-normal 1000))) ;; (save ob-clojure-literate-incanter-plot "/home/stardiviner/Org/Wiki/Computer Technology/Programming/Programming Languages/Clojure/Data/Clojure Packages/data/images/ob-clojure-literate.png") #+end_src ``` Emacs reports error: ``` Not a PNG file: =E2=80=98/home/stardiviner/Org/Wiki/Computer Technology/Programming/Programming Languages/Clojure/Data/Clojure Packages/data/images/ob-clojure-literate.png=E2=80=99 [4 times] ``` But I execute the literate Clojure code: ```clojure (save ob-clojure-literate-incanter-plot "/home/stardiviner/Org/Wiki/Computer Technology/Programming/Programming Languages/Clojure/Data/Clojure Packages/data/images/ob-clojure-literate.png") ``` in CIDER REPL buffer is fine. The plot image is generated correctly. I tried to Edebug the advice function. But can't find out where is wrong. Please someone can help me out. Thanks in advance. [stardiviner] GPG key ID: 47C32433 IRC(freeenode): stardiviner Twitter: @numbchild Key fingerprint =3D 9BAA 92BC CDDD B9EF 3B36 CB99 B8C4 B8E5 47C3 2433 Blog: http://stardiviner.github.io/ --001a11c17f449cb24805653a18c3 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
<= div class=3D"gmail_default" style=3D"font-family: trebuchet\ ms, sans-serif= ;">And here is my final code (some lines commented out for easy debug):
=
```elisp

;;; Support header arguments=C2=A0 :results f= ile :file "image.png"
(defun ob-clojure-literate-inject-code (= args)
=C2=A0 "Inject Clojure code into `BODY' in `ARGS'.
This can be used as :filter-args advice of `org-babel-expand-body:cloj= ure'.
It is used to change Clojure currently working directory and g= enerate graphics image file result."
=C2=A0 (let* ((body (nth 0 arg= s))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (params (nth 1 args= ))
=C2=A0=C2=A0=C2=A0 =C2=A0(dir (cdr (assq :dir params)))
=C2=A0=C2= =A0=C2=A0 =C2=A0(default-directory (and (buffer-file-name) (file-name-direc= tory (buffer-file-name))))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 (directory (and dir (file-name-as-directory (expand-file-name dir))))=C2=A0=C2=A0=C2=A0 =C2=A0(result-type (cdr (assq :results params)))
= =C2=A0=C2=A0=C2=A0 =C2=A0(out-file (cdr (assq :file params)))
=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (add-to-body (lambda (code)
=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (setq body (co= ncat code "\n" body))))
=C2=A0=C2=A0=C2=A0 =C2=A0(append-to-bo= dy (lambda (code)
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2= =A0 =C2=A0=C2=A0 (setq body (concat body "\n" code))))
=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 )
=C2=A0=C2=A0=C2=A0 (unless = (file-directory-p (expand-file-name directory))
=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 (warn (format "ob-clojure-literate directory %s does not exist,= please create it." dir)))
=C2=A0=C2=A0=C2=A0 ;; (when directory=C2=A0=C2=A0=C2=A0 ;;=C2=A0=C2=A0 (funcall add-to-body (format "(Syst= em/setProperty \"user.dir\" \"%s\")" directory)))<= br>=C2=A0=C2=A0=C2=A0 (when (string-match-p (regexp-opt '("graphic= s" "file")) result-type)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;= ; (member "graphics" (cdr (assq :result-params params)))
=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; (funcall add-to-body "(import 'java= .io.FileOutputStream)")
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; use stati= c global variable `ob-clojure-literate-incanter-plot' as convention.=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; (funcall append-to-body
=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 ;; =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 (format "(def ob-clojure-literate-incanter-plot-file (FileOutputSt= ream. \"%s\"))"
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; =C2=A0= =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (conca= t directory out-file)))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; (funcall appen= d-to-body
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; =C2=A0=C2=A0=C2=A0 =C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 "(save ob-clojure-literate-incanter-plot o= b-clojure-literate-incanter-plot-file)")
=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 )
=C2=A0=C2=A0=C2=A0 (when out-file
=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 ;; (funcall append-to-body
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ;; =C2= =A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (format "(view ob= -clojure-literate-incanter-plot)"))

=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 (funcall append-to-body
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0= =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (format "(save ob-clojure-= literate-incanter-plot \"%s\")"
=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 (concat directory out-file)))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 )=C2=A0=C2=A0=C2=A0 (list body params) ; return modified argument list
= =C2=A0=C2=A0=C2=A0 ))

(advice-add 'org-babel-expand-body:clojure= :filter-args #'ob-clojure-literate-inject-code)
```
When I add upper advice code. And execute the following org-babel src blo= ck:
```org
#+begin_src clojure :cache no :dir "data/im= ages" :results graphics :file "ob-clojure-literate.png"
(= use '(incanter core stats datasets charts io pdf))
(def ob-clojure-l= iterate-incanter-plot (histogram (sample-normal 1000)))

;; (save ob-= clojure-literate-incanter-plot "/home/stardiviner/Org/Wiki/Computer Te= chnology/Programming/Programming Languages/Clojure/Data/Clojure Packages/da= ta/images/ob-clojure-literate.png")
#+end_src
```
<= br>Emacs reports error:
```
Not a PNG file: =E2=80=98/home/stardivine= r/Org/Wiki/Computer Technology/Programming/Programming Languages/Clojure/Da= ta/Clojure Packages/data/images/ob-clojure-literate.png=E2=80=99 [4 times]<= br>```

But I execute the literate Clojure code:
<= div class=3D"gmail_default" style=3D"font-family: trebuchet\ ms, sans-serif= ;">```clojure
(save ob-clojure-literate-incanter-plot "/home/stardi= viner/Org/Wiki/Computer Technology/Programming/Programming Languages/Clojur= e/Data/Clojure Packages/data/images/ob-clojure-literate.png")
```
in CIDER REPL buffer is fine. The plot image is generated corre= ctly.

I tried to Edebug the advice function. But= can't find out where is wrong. Please someone can help me out. Thanks = in advance.

[stardiviner]=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 <Hack this world!>= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 GPG key ID: 47C32433
IRC(freeenode): star= diviner =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Twitter:=C2=A0 @numbchild<= br>Key fingerprint =3D 9BAA 92BC CDDD B9EF 3B36=C2=A0 CB99 B8C4 B8E5 47C3 2= 433
Blog: ht= tp://stardiviner.github.io/
--001a11c17f449cb24805653a18c3--