emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] How to pass `format` result string to org-babel-execute:clojure func


From: address@hidden
Subject: [O] How to pass `format` result string to org-babel-execute:clojure function?
Date: Thu, 15 Feb 2018 14:10:48 +0800

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/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]