add license GPL 3
[ob-spice.git] / ob-spice.el
blob8ff01be4906e3f57e4e52035faa4b45b5b8cb91e
1 ;;; ob-spice.el --- org-babel functions for spice evaluation
2 ;;; -*- coding: utf-8 -*-
4 ;; Author: Tiago Oliveira Weber
5 ;; Version: 0.4
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)
14 ;; any later version.
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/>.
24 ;;; Commentary:
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)
29 ;;; Requirements:
31 ;; - ngspice
33 ;;; Code:
34 (require 'ob)
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."
40 ;; example of usage
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))))
50 (setq newbody "");
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 " "))
55 (setq firstword 1)
56 (dolist (word wordlist)
57 (progn ;loop through the words
58 (if (string-match "\\$\\(.*\\)\\[\\(.*\\)\\]" word)
59 (progn
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
64 (setq newword
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))
72 (setq word newword)))
74 ) ; end of (if (string-match "\\$\\(.*\\)\\[\\(.*\\)\\]" word))
75 (if (string-match "\\$\\(.*\\)\\." word) ; if variable has a dot in the end
76 (progn
77 ;; if matchs a non-vector variable format
78 (setq varname (match-string 1 word))
79 (setq newword
80 (assoc-default varname vars
81 (lambda (key candidate)
82 (string= key candidate))))
83 (if (not (eq newword nil))
84 (progn
85 (if (not (stringp newword))
86 (setq newword (number-to-string newword)))
87 (setq word (replace-match (concat newword ".") nil nil word))
88 ;(setq word word)
91 );; end of (if (string-match "\\$\\(.*\\)\\." word)
92 (if (string-match "\\$\\(.*\\)" word)
93 (progn
94 ;; if matchs a non-vector variable format
95 (setq varname (match-string 1 word))
96 (setq newword
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))
103 (setq word newword)
106 ) ; end of (if (string-match "\\$\\(.*\\)" word)
109 (setq newbody (concat newbody
110 (if (not (eq firstword 1)) " ")
111 word))
112 (setq firstword 0)
113 ) ; end of (progn
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 ...)
121 ;;;###autoload
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
129 (mapc (lambda (pair)
130 (when (string= (car pair) "file")
131 (setq textfile (concat (cdr pair) ".txt"))
132 (setq imagefile (concat (cdr pair) ".png"))
135 vars)
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
143 (mapc (lambda (pair)
144 (when (string= (car pair) "file")
145 (setq textfile (concat (cdr pair) ".txt"))
146 (setq imagefile (concat (cdr pair) ".png"))))
147 vars)
148 ;; produce results
149 ;; THE FOLLOWING WAS COMMENTED TEMPORARILY
150 ;; (concat
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 "]]")
155 ;; )
156 ;; )
158 ;; ;; Get measurement values from text-file by splitting comma separated values
159 (if (file-readable-p textfile)
160 (progn
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)
166 (progn
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 "]]")
171 ;;)
173 result
174 ;; Produce output like '(test test2)
175 ;;'(test test2)
180 (provide 'ob-spice)
181 ;;; ob-spice.el ends here