ob-stata.el 12 KB

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