ob-stan.el 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ;;; ob-stan.el --- Babel Functions for Stan -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015-2018 Free Software Foundation, Inc.
  3. ;; Author: Kyle Meyer
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: https://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating Stan [1] source code.
  19. ;;
  20. ;; Evaluating a Stan block can produce two different results.
  21. ;;
  22. ;; 1) Dump the source code contents to a file.
  23. ;;
  24. ;; This file can then be used as a variable in other blocks, which
  25. ;; allows interfaces like RStan to use the model.
  26. ;;
  27. ;; 2) Compile the contents to a model file.
  28. ;;
  29. ;; This provides access to the CmdStan interface. To use this, set
  30. ;; `org-babel-stan-cmdstan-directory' and provide a :file argument
  31. ;; that does not end in ".stan".
  32. ;;
  33. ;; For more information and usage examples, visit
  34. ;; https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-stan.html
  35. ;;
  36. ;; [1] http://mc-stan.org/
  37. ;;; Code:
  38. (require 'ob)
  39. (require 'org-compat)
  40. (defcustom org-babel-stan-cmdstan-directory nil
  41. "CmdStan source directory.
  42. Call \"make\" from this directory to compile the Stan block.
  43. When nil, executing Stan blocks dumps the content to a file."
  44. :group 'org-babel
  45. :type '(choice
  46. (directory :tag "Compilation directory")
  47. (const :tag "Dump to a file" nil)))
  48. (defvar org-babel-default-header-args:stan
  49. '((:results . "file")))
  50. (defun org-babel-execute:stan (body params)
  51. "Generate Stan file from BODY according to PARAMS.
  52. A :file header argument must be given. If
  53. `org-babel-stan-cmdstan-directory' is non-nil and the file name
  54. does not have a \".stan\" extension, save an intermediate
  55. \".stan\" file and compile the block to the named file.
  56. Otherwise, write the Stan code directly to the named file."
  57. (let ((file (expand-file-name
  58. (or (cdr (assq :file params))
  59. (user-error "Set :file argument to execute Stan blocks")))))
  60. (if (or (not org-babel-stan-cmdstan-directory)
  61. (string-match-p "\\.stan\\'" file))
  62. (with-temp-file file (insert body))
  63. (with-temp-file (concat file ".stan") (insert body))
  64. (let ((default-directory org-babel-stan-cmdstan-directory))
  65. (call-process-shell-command (concat "make " file))))
  66. nil)) ; Signal that output has been written to file.
  67. (defun org-babel-prep-session:stan (_session _params)
  68. "Return an error because Stan does not support sessions."
  69. (user-error "Stan does not support sessions"))
  70. (provide 'ob-stan)
  71. ;;; ob-stan.el ends here