ob-asymptote: mixed-types tables default to string arrays

* lisp/ob-asymptote.el (org-babel-asymptote-table-to-array): Require a
new argument TYPE specifying the detected type of array.  If it's a
string array, make sure every element is returned as a string. Also
improve doc-string.
(org-babel-asymptote-var-to-asymptote): Fill new argument.  Small
refactoring.
(org-babel-asymptote-define-type): Rewrite to avoid stopping search at
first float found, as strings have precedence over floats.
This commit is contained in:
Nicolas Goaziou 2011-08-29 17:24:07 +02:00 committed by Eric Schulte
parent d6aa8ffa13
commit 755f597a05
1 changed files with 28 additions and 22 deletions

View File

@ -108,23 +108,29 @@ a variable of the same value."
((stringp val) ((stringp val)
(format "string %S=\"%s\";" var val)) (format "string %S=\"%s\";" var val))
((listp val) ((listp val)
(let* ((dimension-2-p (not (null (cdr val)))) (let* ((dimension-2-p (cdr val))
(dim (if dimension-2-p "[][]" "[]")) (dim (if dimension-2-p "[][]" "[]"))
(type (org-babel-asymptote-define-type val)) (type (org-babel-asymptote-define-type val))
(array (org-babel-asymptote-table-to-array (array (org-babel-asymptote-table-to-array
val val type
(if dimension-2-p '(:lstart "{" :lend "}," :llend "}"))))) (if dimension-2-p '(:lstart "{" :lend "}," :llend "}")))))
(format "%S%s %S=%s;" type dim var array)))))) (format "%S%s %S=%s;" type dim var array))))))
(defun org-babel-asymptote-table-to-array (table params) (defun org-babel-asymptote-table-to-array (table type params)
"Convert values of an elisp table into a string of an asymptote array. "Convert values of TABLE into a string of an asymptote array.
TABLE is a list whose atoms are assumed to be of type
TYPE. PARAMS is a plist of parameters that can influence the
conversion.
Empty cells are ignored." Empty cells are ignored."
(labels ((atom-to-string (table) (labels ((atom-to-string (table)
(cond (cond
((null table) '()) ((null table) '())
((not (listp (car table))) ((not (listp (car table)))
(cons (if (and (stringp (car table)) (cons (if (or (eq type 'string)
(not (string= (car table) ""))) (and (stringp (car table))
(not (string= (car table) ""))))
(format "\"%s\"" (car table)) (format "\"%s\"" (car table))
(format "%s" (car table))) (format "%s" (car table)))
(atom-to-string (cdr table)))) (atom-to-string (cdr table))))
@ -140,22 +146,22 @@ Empty cells are ignored."
(defun org-babel-asymptote-define-type (data) (defun org-babel-asymptote-define-type (data)
"Determine type of DATA. "Determine type of DATA.
DATA is a list. Type symbol is returned as 'symbol. The type is
usually the type of the first atom encountered, except for arrays DATA is a list. Return type as a symbol.
of int, where every cell must be of int type."
(labels ((anything-but-int (el) The type is `string' if any element in DATA is
(cond a string. Otherwise, it is either `float', if some elements are
((null el) nil) floats, or `int'."
((not (listp (car el))) (let* ((type 'int)
(cond (find-type
((floatp (car el)) 'real) (lambda (row)
((stringp (car el)) 'string) (catch 'exit
(t (mapc (lambda (el)
(anything-but-int (cdr el))))) (cond ((listp el) (funcall find-type el))
(t ((stringp el) (throw 'exit (setq type 'string)))
(or (anything-but-int (car el)) ((floatp el) (setq type 'float))))
(anything-but-int (cdr el))))))) row)))))
(or (anything-but-int data) 'int))) (funcall find-type data) type))
(provide 'ob-asymptote) (provide 'ob-asymptote)