1 ;;; ob-spice.el --- org-babel functions for spice evaluation
3 ;; Author: Tiago Oliveira Weber
6 ;; Homepage: http://tiagoweber.github.io
8 ;; Org-Babel support for evaluating spice script.
9 ;; Inspired by Ian Yang's org-export-blocks-format-plantuml (http://www.emacswiki.org/emacs/org-export-blocks-format-plantuml.el)
17 (add-to-list 'org-babel-tangle-lang-exts
'("spice" .
"cir"))
19 (defun ob-spice-concat (wordlist)
20 "Concat elements of a list of string into a string separated by spaces"
22 ;; (ob-spice-concat '("This" "is" "a" "long" "journey"))
23 (setq newtext
(car wordlist
)) ; first word is without space before
24 (setq wordlist
(rest wordlist
)) ; exclude the first word from the list
25 (dolist (word wordlist newtext
) (setq newtext
(concat newtext
" " word
)) ) ;loop through the list and concatenate the values
30 (defun org-babel-expand-body:spice
(body params
)
31 "Expand BODY according to PARAMS, return the expanded body."
33 (vars (mapcar #'cdr
(org-babel-get-header params
:var
)))
37 (setq bodylinelist
(split-string body
"\n"))
38 (dolist (line bodylinelist newbody
) (progn ;loop through list of lines
39 (setq wordlist
(split-string line
" "))
41 (dolist (word wordlist
) (progn ;loop through the words
42 (if (string-match "\\$\\(.*\\)\\[\\(.*\\)\\]" word
)
44 ;; if matchs a vector variable format
45 (setq varname
(match-string 1 word
))
46 (setq varindex
(match-string 2 word
))
47 ;; search varname in vars and use the value of varindex to word
48 (setq newword
(nth (string-to-number varindex
)
50 (assoc-default varname vars
51 (lambda (key candidate
)
52 (string= key candidate
))))))
54 (if (not (eq newword nil
))
55 (if (not (stringp newword
))
56 (setq word
(number-to-string newword
))
60 ) ;; end of (if (string-match "\\$\\(.*\\)\\[\\(.*\\)\\]" word))
61 (if (string-match "\\$\\(.*\\)\\." word
) ;; if variable has a dot in the end
63 ;; if matchs a non-vector variable format
64 (setq varname
(match-string 1 word
))
66 (assoc-default varname vars
67 (lambda (key candidate
)
68 (string= key candidate
))))
69 (if (not (eq newword nil
))
71 (if (not (stringp newword
))
72 (setq newword
(number-to-string newword
)))
73 (setq word
(replace-match (concat newword
".") nil nil word
))
77 );; end of (if (string-match "\\$\\(.*\\)\\." word)
78 (if (string-match "\\$\\(.*\\)" word
)
80 ;; if matchs a non-vector variable format
81 (setq varname
(match-string 1 word
))
83 (assoc-default varname vars
84 (lambda (key candidate
)
85 (string= key candidate
))))
86 (if (not (eq newword nil
))
87 (if (not (stringp newword
))
88 (setq word
(number-to-string newword
))
92 );; end of (if (string-match "\\$\\(.*\\)" word)
95 (setq newbody
(concat newbody
96 (if (not (eq firstword
1)) " ")
100 ) ;; end of (dolist (word wordlist))
102 (setq newbody
(concat newbody
"\n"))
103 ) ;; end of (progn ;; loop through list of lines ... )
104 ) ;; end of (dolist (line bodylinelist) ...function ...)
108 (defun org-babel-execute:spice
(body params
)
109 "Execute a block of Spice code with org-babel."
110 (let ((body (org-babel-expand-body:spice body params
))
111 (vars (mapcar #'cdr
(org-babel-get-header params
:var
)))
114 ;;******************************
115 ;; clean temporary files
117 (when (string= (car pair
) "file")
118 (setq textfile
(concat (cdr pair
) ".txt"))
119 (setq imagefile
(concat (cdr pair
) ".png"))
123 ;; (if (file-readable-p textfile) (delete-file textfile))
124 ;; (if (file-readable-p imagefile) (delete-file imagefile))
125 ;;*******************************
127 (org-babel-eval "ngspice -b " body
)
129 ;; loop through all pairs (elements) of the list vars and set text and image file if finds "file" var
131 (when (string= (car pair
) "file")
132 (setq textfile
(concat (cdr pair
) ".txt"))
133 (setq imagefile
(concat (cdr pair
) ".png"))
138 ;; THE FOLLOWING WAS COMMENTED TEMPORARILY
140 ;; (if (file-readable-p textfile)
141 ;; (get-string-from-file textfile))
142 ;; (if (file-readable-p imagefile)
143 ;; (concat '"#+ATTR_HTML: :width 600px \n [[file:./" imagefile "]]")
147 ;; ;; Get measurement values from text-file by splitting comma separated values
148 (if (file-readable-p textfile
)
150 (setq rawtext
(get-string-from-file textfile
))
151 ;;(setq rawtext (replace-regexp-in-string "\n" "" rawtext))
152 (setq rawtext
(replace-regexp-in-string "\n" "" rawtext
))
153 (setq result
(split-string rawtext
","))
156 (if (file-readable-p imagefile
)
158 ;; test if result exist already
159 ;;(if (boundp 'result)
160 (add-to-list 'result
(concat '"[[file:./" imagefile
"]]") t
) ;; add imagefile to last entry
161 ;;(concat '"[[file:./" imagefile "]]")
166 ;; Produce output like '(test test2)
173 ;;; ob-spice.el ends here