emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How to pass `format` result string to org-babel-execute:clojure function?
@ 2018-02-15  6:10 numbchild
  0 siblings, 0 replies; only message in thread
From: numbchild @ 2018-02-15  6:10 UTC (permalink / raw)
  To: Org-mode

[-- Attachment #1: Type: text/plain, Size: 3966 bytes --]

I try add advice as I posted at here
https://emacs.stackexchange.com/questions/38818/how-to-write-this-advice-for-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: ‘/home/stardiviner/Org/Wiki/Computer
Technology/Programming/Programming Languages/Clojure/Data/Clojure
Packages/data/images/ob-clojure-literate.png’ [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]           <Hack this world!>      GPG key ID: 47C32433
IRC(freeenode): stardiviner                     Twitter:  @numbchild
Key fingerprint = 9BAA 92BC CDDD B9EF 3B36  CB99 B8C4 B8E5 47C3 2433
Blog: http://stardiviner.github.io/

[-- Attachment #2: Type: text/html, Size: 5946 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2018-02-15  6:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-15  6:10 How to pass `format` result string to org-babel-execute:clojure function? numbchild

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).