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