Merge branch 'maint'
[org-mode.git] / contrib / lisp / ob-stata.el
blobd8cf52a3fa93866304c2452f2a8bb7c4a3ae49a3
1 ;;; ob-stata.el --- org-babel functions for stata code evaluation
3 ;; Copyright (C) 2014 Ista Zahn
4 ;; Author: Ista Zahn istazahn@gmail.com
5 ;; G. Jay Kerns
6 ;; Eric Schulte
7 ;; Dan Davison
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)
15 ;; any later version.
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.
27 ;;; Commentary:
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.
37 ;; --Ista, 07/30/2014
39 ;;; Requirements:
40 ;; Stata: http://stata.com
41 ;; ESS: http://ess.r-project.org
43 ;;; Code:
44 (require 'ob)
45 (require 'cl-lib)
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))
54 (defconst org-babel-header-args:stata
55 '((width . :any)
56 (horizontal . :any)
57 (results . ((file list vector table scalar verbatim)
58 (raw org html latex code pp wrap)
59 (replace silent append prepend)
60 ;; NOTE: not sure 'value' makes sense in stata
61 ;; we may want to remove it from the list
62 (output value graphics))))
63 "stata-specific header arguments.")
65 (add-to-list 'org-babel-tangle-lang-exts '("stata" . "do"))
67 ;; only ':results output' currently works, so make that the default
68 (defvar org-babel-default-header-args:stata '((:results . "output")))
70 (defcustom org-babel-stata-command inferior-STA-program-name
71 "Name of command to use for executing stata code."
72 :group 'org-babel
73 :version "24.4"
74 :package-version '(Org . "8.3")
75 :type 'string)
77 (defvar ess-local-process-name) ; dynamically scoped
78 (defun org-babel-edit-prep:stata (info)
79 (let ((session (cdr (assq :session (nth 2 info)))))
80 (when (and session (string-match "^\\*\\(.+?\\)\\*$" session))
81 (save-match-data (org-babel-stata-initiate-session session nil)))))
83 (defun org-babel-expand-body:stata (body params &optional graphics-file)
84 "Expand BODY according to PARAMS, return the expanded body."
85 (let ((graphics-file
86 (or graphics-file (org-babel-stata-graphical-output-file params))))
87 (mapconcat
88 #'identity
89 ((lambda (inside)
90 (if graphics-file
91 inside
92 inside))
93 (append (org-babel-variable-assignments:stata params)
94 (list body))) "\n")))
96 (defun org-babel-execute:stata (body params)
97 "Execute a block of stata code.
98 This function is called by `org-babel-execute-src-block'."
99 (save-excursion
100 (let* ((result-params (cdr (assq :result-params params)))
101 (result-type (cdr (assq :result-type params)))
102 (session (org-babel-stata-initiate-session
103 (cdr (assq :session params)) params))
104 (colnames-p (cdr (assq :colnames params)))
105 (rownames-p (cdr (assq :rownames params)))
106 (graphics-file (org-babel-stata-graphical-output-file params))
107 (full-body (org-babel-expand-body:stata body params graphics-file))
108 (result
109 (org-babel-stata-evaluate
110 session full-body result-type result-params
111 (or (equal "yes" colnames-p)
112 (org-babel-pick-name
113 (cdr (assq :colname-names params)) colnames-p))
114 (or (equal "yes" rownames-p)
115 (org-babel-pick-name
116 (cdr (assq :rowname-names params)) rownames-p)))))
117 (if graphics-file nil result))))
119 (defun org-babel-prep-session:stata (session params)
120 "Prepare SESSION according to the header arguments specified in PARAMS."
121 (let* ((session (org-babel-stata-initiate-session session params))
122 (var-lines (org-babel-variable-assignments:stata params)))
123 (org-babel-comint-in-buffer session
124 (mapc (lambda (var)
125 (end-of-line 1) (insert var) (comint-send-input nil t)
126 (org-babel-comint-wait-for-output session)) var-lines))
127 session))
129 (defun org-babel-load-session:stata (session body params)
130 "Load BODY into SESSION."
131 (save-window-excursion
132 (let ((buffer (org-babel-prep-session:stata session params)))
133 (with-current-buffer buffer
134 (goto-char (process-mark (get-buffer-process (current-buffer))))
135 (insert (org-babel-chomp body)))
136 buffer)))
138 ;; helper functions
140 (defun org-babel-variable-assignments:stata (params)
141 "Return list of stata statements assigning the block's variables."
142 (let ((vars (org-babel--get-vars params)))
143 (mapcar
144 (lambda (pair)
145 (org-babel-stata-assign-elisp
146 (car pair) (cdr pair)
147 (equal "yes" (cdr (assq :colnames params)))
148 (equal "yes" (cdr (assq :rownames params)))))
149 (mapcar
150 (lambda (i)
151 (cons (car (nth i vars))
152 (org-babel-reassemble-table
153 (cdr (nth i vars))
154 (cdr (nth i (cdr (assq :colname-names params))))
155 (cdr (nth i (cdr (assq :rowname-names params)))))))
156 (org-number-sequence 0 (1- (length vars)))))))
158 (defun org-babel-stata-quote-csv-field (s)
159 "Quote field S for export to stata."
160 (if (stringp s)
161 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
162 (format "%S" s)))
164 (defun org-babel-stata-assign-elisp (name value colnames-p rownames-p)
165 "Construct stata code assigning the elisp VALUE to a variable named NAME."
166 (if (listp value)
167 (let ((max (apply #'max (mapcar #'length (cl-remove-if-not
168 #'sequencep value))))
169 (min (apply #'min (mapcar #'length (cl-remove-if-not
170 #'sequencep value))))
171 (transition-file (org-babel-temp-file "stata-import-")))
172 ;; ensure VALUE has an orgtbl structure (depth of at least 2)
173 (unless (listp (car value)) (setq value (list value)))
174 (with-temp-file transition-file
175 (insert
176 (orgtbl-to-csv value '(:fmt org-babel-stata-quote-csv-field))
177 "\n"))
178 (let ((file (org-babel-process-file-name transition-file 'noquote))
179 (header (if (or (eq (nth 1 value) 'hline) colnames-p)
180 "TRUE" "FALSE"))
181 (row-names (if rownames-p "1" "NULL")))
182 (if (= max min)
183 (format "%s = insheet using \"%s\"" name file)
184 (format "%s = insheet using \"%s\""
185 name file))))
186 (format "%s = %s" name (org-babel-stata-quote-csv-field value))))
188 (defvar ess-ask-for-ess-directory) ; dynamically scoped
190 (defun org-babel-stata-initiate-session (session params)
191 "If there is not a current stata process then create one."
192 (unless (string= session "none")
193 (let ((session (or session "*stata*"))
194 (ess-ask-for-ess-directory
195 (and (and (boundp 'ess-ask-for-ess-directory) ess-ask-for-ess-directory)
196 (not (cdr (assq :dir params))))))
197 (if (org-babel-comint-buffer-livep session)
198 session
199 (save-window-excursion
200 (require 'ess) (stata)
201 (rename-buffer
202 (if (bufferp session)
203 (buffer-name session)
204 (if (stringp session)
205 session
206 (buffer-name))))
207 (current-buffer))))))
209 (defun org-babel-stata-associate-session (session)
210 "Associate stata code buffer with a stata session.
211 Make SESSION be the inferior ESS process associated with the
212 current code buffer."
213 (setq ess-local-process-name
214 (process-name (get-buffer-process session)))
215 (ess-make-buffer-current))
217 (defun org-babel-stata-graphical-output-file (params)
218 "Name of file to which stata should send graphical output."
219 (and (member "graphics" (cdr (assq :result-params params)))
220 (cdr (assq :file params))))
222 (defvar org-babel-stata-eoe-indicator "display \"org_babel_stata_eoe\"")
223 (defvar org-babel-stata-eoe-output "org_babel_stata_eoe")
225 (defvar org-babel-stata-write-object-command "outsheet using \"%s\"")
227 (defun org-babel-stata-evaluate
228 (session body result-type result-params column-names-p row-names-p)
229 "Evaluate stata code in BODY."
230 (if session
231 (org-babel-stata-evaluate-session
232 session body result-type result-params column-names-p row-names-p)
233 (org-babel-stata-evaluate-external-process
234 body result-type result-params column-names-p row-names-p)))
236 (defun org-babel-stata-evaluate-external-process
237 (body result-type result-params column-names-p row-names-p)
238 "Evaluate BODY in external stata process.
239 If RESULT-TYPE equals 'output then return standard output as a
240 string. If RESULT-TYPE equals 'value then return the value of the
241 last statement in BODY, as elisp."
242 (cl-case result-type
243 (value
244 (let ((tmp-file (org-babel-temp-file "stata-")))
245 (org-babel-eval org-babel-stata-command
246 (format org-babel-stata-write-object-command
247 (org-babel-process-file-name tmp-file 'noquote)
248 (format "begin\n%s\nend" body)))
249 (org-babel-stata-process-value-result
250 (org-babel-result-cond result-params
251 (with-temp-buffer
252 (insert-file-contents tmp-file)
253 (buffer-string))
254 (org-babel-import-elisp-from-file tmp-file '(4)))
255 column-names-p)))
256 (output (org-babel-eval org-babel-stata-command body))))
258 (defun org-babel-stata-evaluate-session
259 (session body result-type result-params column-names-p row-names-p)
260 "Evaluate BODY in SESSION.
261 If RESULT-TYPE equals 'output then return standard output as a
262 string. If RESULT-TYPE equals 'value then return the value of the
263 last statement in BODY, as elisp."
264 (cl-case result-type
265 (value
266 (with-temp-buffer
267 (insert (org-babel-chomp body))
268 (let ((ess-local-process-name
269 (process-name (get-buffer-process session)))
270 (ess-eval-visibly-p nil))
271 (ess-eval-buffer nil)))
272 (let ((tmp-file (org-babel-temp-file "stata-")))
273 (org-babel-comint-eval-invisibly-and-wait-for-file
274 session tmp-file
275 (format org-babel-stata-write-object-command
276 (org-babel-process-file-name tmp-file 'noquote) "ans"))
277 (org-babel-stata-process-value-result
278 (org-babel-result-cond result-params
279 (with-temp-buffer
280 (insert-file-contents tmp-file)
281 (buffer-string))
282 (org-babel-import-elisp-from-file tmp-file '(4)))
283 column-names-p)))
284 (output
285 (mapconcat
286 #'org-babel-chomp
287 (butlast
288 (delq nil
289 (mapcar
290 (lambda (line) (when (> (length line) 0) line))
291 (mapcar
292 (lambda (line) ;; cleanup extra prompts left in output
293 (if (string-match
294 "^\\([ ]*[>+\\.][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line)
295 (substring line (match-end 1))
296 line))
297 (org-babel-comint-with-output (session org-babel-stata-eoe-output)
298 (insert (mapconcat #'org-babel-chomp
299 (list body org-babel-stata-eoe-indicator)
300 "\n"))
301 (inferior-ess-send-input)))))) "\n"))))
303 (defun org-babel-stata-process-value-result (result column-names-p)
304 "stata-specific processing of return value.
305 Insert hline if column names in output have been requested."
306 (if column-names-p
307 (cons (car result) (cons 'hline (cdr result)))
308 result))
310 (provide 'ob-stata)
312 ;;; ob-stata.el ends here