ob-stata.el 12 KB

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