I try add advice as I posted at here

And here is my final code (some lines commented out for easy debug):


;;; 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
         (directory (and dir (file-name-as-directory (expand-file-name
     (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
      ;; (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
    (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

When I add upper advice code. And execute the following org-babel src block:
#+begin_src clojure :cache no :dir "data/images" :results graphics :file
(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

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:
(save ob-clojure-literate-incanter-plot
"/home/stardiviner/Org/Wiki/Computer Technology/Programming/Programming
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