1 ;;; ob-spice.el --- org-babel functions for spice evaluation
2 ;;; -*- coding: utf-8 -*-
4 ;; Author: Tiago Oliveira Weber
6 ;; Package-Requires: ((spice-mode "0.0.1") (org "8"))
7 ;; Homepage: http://tiagoweber.github.io
9 ;; License: GPL v3, or any later version
11 ;; This file is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
26 ;; Org-Babel support for evaluating spice script.
27 ;; Inspired by Ian Yang's org-export-blocks-format-plantuml (http://www.emacswiki.org/emacs/org-export-blocks-format-plantuml.el)
36 (add-to-list 'org-babel-tangle-lang-exts
'("spice" .
"cir"))
38 (defun ob-spice-concat (wordlist)
39 "Concatenate elements of a `WORDLIST' into a string separated by spaces."
41 ;; (ob-spice-concat '("This" "is" "a" "long" "journey"))
42 (setq newtext
(car wordlist
)) ; first word is without space before
43 (setq wordlist
(rest wordlist
)) ; exclude the first word from the list
44 (dolist (word wordlist newtext
) ; loop through the list and concatenate the values
45 (setq newtext
(concat newtext
" " word
))))
47 (defun org-babel-expand-body:spice
(body params
)
48 "Expand BODY according to PARAMS, return the expanded body."
49 (let* ((vars (mapcar #'cdr
(org-babel-get-header params
:var
))))
51 (setq bodylinelist
(split-string body
"\n"))
52 (dolist (line bodylinelist newbody
)
53 (progn ;loop through list of lines
54 (setq wordlist
(split-string line
" "))
56 (dolist (word wordlist
)
57 (progn ;loop through the words
58 (if (string-match "\\$\\(.*\\)\\[\\(.*\\)\\]" word
)
60 ;; if matchs a vector variable format
61 (setq varname
(match-string 1 word
))
62 (setq varindex
(match-string 2 word
))
63 ;; search varname in vars and use the value of varindex to word
65 (nth (string-to-number varindex
)
66 (car (assoc-default varname vars
67 (lambda (key candidate
)
68 (string= key candidate
))))))
69 (if (not (eq newword nil
))
70 (if (not (stringp newword
))
71 (setq word
(number-to-string newword
))
74 ) ; end of (if (string-match "\\$\\(.*\\)\\[\\(.*\\)\\]" word))
75 (if (string-match "\\$\\(.*\\)\\." word
) ; if variable has a dot in the end
77 ;; if matchs a non-vector variable format
78 (setq varname
(match-string 1 word
))
80 (assoc-default varname vars
81 (lambda (key candidate
)
82 (string= key candidate
))))
83 (if (not (eq newword nil
))
85 (if (not (stringp newword
))
86 (setq newword
(number-to-string newword
)))
87 (setq word
(replace-match (concat newword
".") nil nil word
))
91 );; end of (if (string-match "\\$\\(.*\\)\\." word)
92 (if (string-match "\\$\\(.*\\)" word
)
94 ;; if matchs a non-vector variable format
95 (setq varname
(match-string 1 word
))
97 (assoc-default varname vars
98 (lambda (key candidate
)
99 (string= key candidate
))))
100 (if (not (eq newword nil
))
101 (if (not (stringp newword
))
102 (setq word
(number-to-string newword
))
106 ) ; end of (if (string-match "\\$\\(.*\\)" word)
109 (setq newbody
(concat newbody
110 (if (not (eq firstword
1)) " ")
114 ) ; end of (dolist (word wordlist))
116 (setq newbody
(concat newbody
"\n"))
117 ) ; end of (progn ;; loop through list of lines ... )
118 ) ; end of (dolist (line bodylinelist) ...function ...)
122 (defun org-babel-execute:spice
(body params
)
123 "Execute a block of Spice code `BODY' with org-babel and `PARAMS'."
124 (let ((body (org-babel-expand-body:spice body params
))
125 (vars (mapcar #'cdr
(org-babel-get-header params
:var
))))
127 ;;******************************
128 ;; clean temporary files
130 (when (string= (car pair
) "file")
131 (setq textfile
(concat (cdr pair
) ".txt"))
132 (setq imagefile
(concat (cdr pair
) ".png"))
136 ;; (if (file-readable-p textfile) (delete-file textfile))
137 ;; (if (file-readable-p imagefile) (delete-file imagefile))
138 ;;*******************************
140 (org-babel-eval "ngspice -b " body
)
142 ;; loop through all pairs (elements) of the list vars and set text and image file if finds "file" var
144 (when (string= (car pair
) "file")
145 (setq textfile
(concat (cdr pair
) ".txt"))
146 (setq imagefile
(concat (cdr pair
) ".png"))))
149 ;; THE FOLLOWING WAS COMMENTED TEMPORARILY
151 ;; (if (file-readable-p textfile)
152 ;; (get-string-from-file textfile))
153 ;; (if (file-readable-p imagefile)
154 ;; (concat '"#+ATTR_HTML: :width 600px \n [[file:./" imagefile "]]")
158 ;; ;; Get measurement values from text-file by splitting comma separated values
159 (if (file-readable-p textfile
)
161 (setq rawtext
(get-string-from-file textfile
))
162 ;;(setq rawtext (replace-regexp-in-string "\n" "" rawtext))
163 (setq rawtext
(replace-regexp-in-string "\n" "" rawtext
))
164 (setq result
(split-string rawtext
","))))
165 (if (file-readable-p imagefile
)
167 ;; test if result exist already
168 ;;(if (boundp 'result)
169 (add-to-list 'result
(concat '"[[file:./" imagefile
"]]") t
) ;; add imagefile to last entry
170 ;;(concat '"[[file:./" imagefile "]]")
174 ;; Produce output like '(test test2)
181 ;;; ob-spice.el ends here