1 ;;; ob-stata.el --- org-babel functions for stata code evaluation
3 ;; Copyright (C) 2014 Ista Zahn
4 ;; Author: Ista Zahn istazahn@gmail.com
10 ;; This file is not part of GNU Emacs.
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; The file provides Org-Babel support for evaluating stata code.
30 ;; It is basically result of find-and-replace "stata" for "julia"
31 ;; in ob-julia.el by G. Jay Kerns. Only ":results output" works: the
32 ;; header args must include ":results output" (this is the default).
33 ;; Note that I'm not sure ':results value' makes sense or is useful
34 ;; but I have left all the value-processing stuff inherited from
35 ;; ob-julia and ob-R. ':results graphics' would be nice, but I have
36 ;; not tried to implement it.
40 ;; Stata: http://stata.com
41 ;; ESS: http://ess.r-project.org
45 (eval-when-compile (require 'cl
))
47 (declare-function orgtbl-to-csv
"org-table" (table params
))
48 (declare-function stata
"ext:ess-stata" (&optional start-args
))
49 (declare-function inferior-ess-send-input
"ext:ess-inf" ())
50 (declare-function ess-make-buffer-current
"ext:ess-inf" ())
51 (declare-function ess-eval-buffer
"ext:ess-inf" (vis))
52 (declare-function org-number-sequence
"org-compat" (from &optional to inc
))
53 (declare-function org-remove-if-not
"org" (predicate seq
))
55 (defconst org-babel-header-args
:stata
58 (results .
((file list vector table scalar verbatim
)
59 (raw org html latex code pp wrap
)
60 (replace silent append prepend
)
61 ;; NOTE: not sure 'value' makes sense in stata
62 ;; we may want to remove it from the list
63 (output value graphics
))))
64 "stata-specific header arguments.")
66 (add-to-list 'org-babel-tangle-lang-exts
'("stata" .
"do"))
68 ;; only ':results output' currently works, so make that the default
69 (defvar org-babel-default-header-args
:stata
'((:results .
"output")))
71 (defcustom org-babel-stata-command inferior-STA-program-name
72 "Name of command to use for executing stata code."
75 :package-version
'(Org .
"8.3")
78 (defvar ess-local-process-name
) ; dynamically scoped
79 (defun org-babel-edit-prep:stata
(info)
80 (let ((session (cdr (assoc :session
(nth 2 info
)))))
81 (when (and session
(string-match "^\\*\\(.+?\\)\\*$" session
))
82 (save-match-data (org-babel-stata-initiate-session session nil
)))))
84 (defun org-babel-expand-body:stata
(body params
&optional graphics-file
)
85 "Expand BODY according to PARAMS, return the expanded body."
87 (or graphics-file
(org-babel-stata-graphical-output-file params
))))
94 (append (org-babel-variable-assignments:stata params
)
97 (defun org-babel-execute:stata
(body params
)
98 "Execute a block of stata code.
99 This function is called by `org-babel-execute-src-block'."
101 (let* ((result-params (cdr (assoc :result-params params
)))
102 (result-type (cdr (assoc :result-type params
)))
103 (session (org-babel-stata-initiate-session
104 (cdr (assoc :session params
)) params
))
105 (colnames-p (cdr (assoc :colnames params
)))
106 (rownames-p (cdr (assoc :rownames params
)))
107 (graphics-file (org-babel-stata-graphical-output-file params
))
108 (full-body (org-babel-expand-body:stata body params graphics-file
))
110 (org-babel-stata-evaluate
111 session full-body result-type result-params
112 (or (equal "yes" colnames-p
)
114 (cdr (assoc :colname-names params
)) colnames-p
))
115 (or (equal "yes" rownames-p
)
117 (cdr (assoc :rowname-names params
)) rownames-p
)))))
118 (if graphics-file nil result
))))
120 (defun org-babel-prep-session:stata
(session params
)
121 "Prepare SESSION according to the header arguments specified in PARAMS."
122 (let* ((session (org-babel-stata-initiate-session session params
))
123 (var-lines (org-babel-variable-assignments:stata params
)))
124 (org-babel-comint-in-buffer session
126 (end-of-line 1) (insert var
) (comint-send-input nil t
)
127 (org-babel-comint-wait-for-output session
)) var-lines
))
130 (defun org-babel-load-session:stata
(session body params
)
131 "Load BODY into SESSION."
132 (save-window-excursion
133 (let ((buffer (org-babel-prep-session:stata session params
)))
134 (with-current-buffer buffer
135 (goto-char (process-mark (get-buffer-process (current-buffer))))
136 (insert (org-babel-chomp body
)))
141 (defun org-babel-variable-assignments:stata
(params)
142 "Return list of stata statements assigning the block's variables."
143 (let ((vars (mapcar #'cdr
(org-babel-get-header params
:var
))))
146 (org-babel-stata-assign-elisp
147 (car pair
) (cdr pair
)
148 (equal "yes" (cdr (assoc :colnames params
)))
149 (equal "yes" (cdr (assoc :rownames params
)))))
152 (cons (car (nth i vars
))
153 (org-babel-reassemble-table
155 (cdr (nth i
(cdr (assoc :colname-names params
))))
156 (cdr (nth i
(cdr (assoc :rowname-names params
)))))))
157 (org-number-sequence 0 (1- (length vars
)))))))
159 (defun org-babel-stata-quote-csv-field (s)
160 "Quote field S for export to stata."
162 (concat "\"" (mapconcat 'identity
(split-string s
"\"") "\"\"") "\"")
165 (defun org-babel-stata-assign-elisp (name value colnames-p rownames-p
)
166 "Construct stata code assigning the elisp VALUE to a variable named NAME."
168 (let ((max (apply #'max
(mapcar #'length
(org-remove-if-not
169 #'sequencep value
))))
170 (min (apply #'min
(mapcar #'length
(org-remove-if-not
171 #'sequencep value
))))
172 (transition-file (org-babel-temp-file "stata-import-")))
173 ;; ensure VALUE has an orgtbl structure (depth of at least 2)
174 (unless (listp (car value
)) (setq value
(list value
)))
175 (with-temp-file transition-file
177 (orgtbl-to-csv value
'(:fmt org-babel-stata-quote-csv-field
))
179 (let ((file (org-babel-process-file-name transition-file
'noquote
))
180 (header (if (or (eq (nth 1 value
) 'hline
) colnames-p
)
182 (row-names (if rownames-p
"1" "NULL")))
184 (format "%s = insheet using \"%s\"" name file
)
185 (format "%s = insheet using \"%s\""
187 (format "%s = %s" name
(org-babel-stata-quote-csv-field value
))))
189 (defvar ess-ask-for-ess-directory
) ; dynamically scoped
191 (defun org-babel-stata-initiate-session (session params
)
192 "If there is not a current stata process then create one."
193 (unless (string= session
"none")
194 (let ((session (or session
"*stata*"))
195 (ess-ask-for-ess-directory
196 (and (and (boundp 'ess-ask-for-ess-directory
) ess-ask-for-ess-directory
)
197 (not (cdr (assoc :dir params
))))))
198 (if (org-babel-comint-buffer-livep session
)
200 (save-window-excursion
201 (require 'ess
) (stata)
203 (if (bufferp session
)
204 (buffer-name session
)
205 (if (stringp session
)
208 (current-buffer))))))
210 (defun org-babel-stata-associate-session (session)
211 "Associate stata code buffer with a stata session.
212 Make SESSION be the inferior ESS process associated with the
213 current code buffer."
214 (setq ess-local-process-name
215 (process-name (get-buffer-process session
)))
216 (ess-make-buffer-current))
218 (defun org-babel-stata-graphical-output-file (params)
219 "Name of file to which stata should send graphical output."
220 (and (member "graphics" (cdr (assq :result-params params
)))
221 (cdr (assq :file params
))))
223 (defvar org-babel-stata-eoe-indicator
"display \"org_babel_stata_eoe\"")
224 (defvar org-babel-stata-eoe-output
"org_babel_stata_eoe")
226 (defvar org-babel-stata-write-object-command
"outsheet using \"%s\"")
228 (defun org-babel-stata-evaluate
229 (session body result-type result-params column-names-p row-names-p
)
230 "Evaluate stata code in BODY."
232 (org-babel-stata-evaluate-session
233 session body result-type result-params column-names-p row-names-p
)
234 (org-babel-stata-evaluate-external-process
235 body result-type result-params column-names-p row-names-p
)))
237 (defun org-babel-stata-evaluate-external-process
238 (body result-type result-params column-names-p row-names-p
)
239 "Evaluate BODY in external stata process.
240 If RESULT-TYPE equals 'output then return standard output as a
241 string. If RESULT-TYPE equals 'value then return the value of the
242 last statement in BODY, as elisp."
245 (let ((tmp-file (org-babel-temp-file "stata-")))
246 (org-babel-eval org-babel-stata-command
247 (format org-babel-stata-write-object-command
248 (org-babel-process-file-name tmp-file
'noquote
)
249 (format "begin\n%s\nend" body
)))
250 (org-babel-stata-process-value-result
251 (org-babel-result-cond result-params
253 (insert-file-contents tmp-file
)
255 (org-babel-import-elisp-from-file tmp-file
'(4)))
257 (output (org-babel-eval org-babel-stata-command body
))))
259 (defun org-babel-stata-evaluate-session
260 (session body result-type result-params column-names-p row-names-p
)
261 "Evaluate BODY in SESSION.
262 If RESULT-TYPE equals 'output then return standard output as a
263 string. If RESULT-TYPE equals 'value then return the value of the
264 last statement in BODY, as elisp."
268 (insert (org-babel-chomp body
))
269 (let ((ess-local-process-name
270 (process-name (get-buffer-process session
)))
271 (ess-eval-visibly-p nil
))
272 (ess-eval-buffer nil
)))
273 (let ((tmp-file (org-babel-temp-file "stata-")))
274 (org-babel-comint-eval-invisibly-and-wait-for-file
276 (format org-babel-stata-write-object-command
277 (org-babel-process-file-name tmp-file
'noquote
) "ans"))
278 (org-babel-stata-process-value-result
279 (org-babel-result-cond result-params
281 (insert-file-contents tmp-file
)
283 (org-babel-import-elisp-from-file tmp-file
'(4)))
291 (lambda (line) (when (> (length line
) 0) line
))
293 (lambda (line) ;; cleanup extra prompts left in output
295 "^\\([ ]*[>+\\.][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line
)
296 (substring line
(match-end 1))
298 (org-babel-comint-with-output (session org-babel-stata-eoe-output
)
299 (insert (mapconcat #'org-babel-chomp
300 (list body org-babel-stata-eoe-indicator
)
302 (inferior-ess-send-input)))))) "\n"))))
304 (defun org-babel-stata-process-value-result (result column-names-p
)
305 "stata-specific processing of return value.
306 Insert hline if column names in output have been requested."
308 (cons (car result
) (cons 'hline
(cdr result
)))
313 ;;; ob-stata.el ends here