org-table: Fix durations extracted from node properties
[org-mode.git] / contrib / lisp / ob-stata.el
blob29aa88d01ed536482e81cfda35829698b3763e4c
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 (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
56 '((width . :any)
57 (horizontal . :any)
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."
73 :group 'org-babel
74 :version "24.4"
75 :package-version '(Org . "8.3")
76 :type 'string)
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."
86 (let ((graphics-file
87 (or graphics-file (org-babel-stata-graphical-output-file params))))
88 (mapconcat
89 #'identity
90 ((lambda (inside)
91 (if graphics-file
92 inside
93 inside))
94 (append (org-babel-variable-assignments:stata params)
95 (list body))) "\n")))
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'."
100 (save-excursion
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))
109 (result
110 (org-babel-stata-evaluate
111 session full-body result-type result-params
112 (or (equal "yes" colnames-p)
113 (org-babel-pick-name
114 (cdr (assoc :colname-names params)) colnames-p))
115 (or (equal "yes" rownames-p)
116 (org-babel-pick-name
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
125 (mapc (lambda (var)
126 (end-of-line 1) (insert var) (comint-send-input nil t)
127 (org-babel-comint-wait-for-output session)) var-lines))
128 session))
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)))
137 buffer)))
139 ;; helper functions
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))))
144 (mapcar
145 (lambda (pair)
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)))))
150 (mapcar
151 (lambda (i)
152 (cons (car (nth i vars))
153 (org-babel-reassemble-table
154 (cdr (nth i vars))
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."
161 (if (stringp s)
162 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
163 (format "%S" 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."
167 (if (listp value)
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
176 (insert
177 (orgtbl-to-csv value '(:fmt org-babel-stata-quote-csv-field))
178 "\n"))
179 (let ((file (org-babel-process-file-name transition-file 'noquote))
180 (header (if (or (eq (nth 1 value) 'hline) colnames-p)
181 "TRUE" "FALSE"))
182 (row-names (if rownames-p "1" "NULL")))
183 (if (= max min)
184 (format "%s = insheet using \"%s\"" name file)
185 (format "%s = insheet using \"%s\""
186 name file))))
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)
199 session
200 (save-window-excursion
201 (require 'ess) (stata)
202 (rename-buffer
203 (if (bufferp session)
204 (buffer-name session)
205 (if (stringp session)
206 session
207 (buffer-name))))
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."
231 (if session
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."
243 (case result-type
244 (value
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
252 (with-temp-buffer
253 (insert-file-contents tmp-file)
254 (buffer-string))
255 (org-babel-import-elisp-from-file tmp-file '(4)))
256 column-names-p)))
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."
265 (case result-type
266 (value
267 (with-temp-buffer
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
275 session tmp-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
280 (with-temp-buffer
281 (insert-file-contents tmp-file)
282 (buffer-string))
283 (org-babel-import-elisp-from-file tmp-file '(4)))
284 column-names-p)))
285 (output
286 (mapconcat
287 #'org-babel-chomp
288 (butlast
289 (delq nil
290 (mapcar
291 (lambda (line) (when (> (length line) 0) line))
292 (mapcar
293 (lambda (line) ;; cleanup extra prompts left in output
294 (if (string-match
295 "^\\([ ]*[>+\\.][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line)
296 (substring line (match-end 1))
297 line))
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)
301 "\n"))
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."
307 (if column-names-p
308 (cons (car result) (cons 'hline (cdr result)))
309 result))
311 (provide 'ob-stata)
313 ;;; ob-stata.el ends here