Fix `org-set-visibility-according-to-property'
[org-mode/org-kjn.git] / lisp / ob-haskell.el
blob00066702a519b051fe108720d47d132fc637343c
1 ;;; ob-haskell.el --- org-babel functions for haskell evaluation
3 ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://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 <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating haskell source code. This one will
27 ;; be sort of tricky because haskell programs must be compiled before
28 ;; they can be run, but haskell code can also be run through an
29 ;; interactive interpreter.
31 ;; For now lets only allow evaluation using the haskell interpreter.
33 ;;; Requirements:
35 ;; - haskell-mode :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
37 ;; - inf-haskell :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
39 ;; - (optionally) lhs2tex :: http://people.cs.uu.nl/andres/lhs2tex/
41 ;;; Code:
42 (require 'ob)
43 (require 'comint)
44 (eval-when-compile (require 'cl))
46 (declare-function org-remove-indentation "org" (code &optional n))
47 (declare-function haskell-mode "ext:haskell-mode" ())
48 (declare-function run-haskell "ext:inf-haskell" (&optional arg))
49 (declare-function inferior-haskell-load-file
50 "ext:inf-haskell" (&optional reload))
52 (defvar org-babel-tangle-lang-exts)
53 (add-to-list 'org-babel-tangle-lang-exts '("haskell" . "hs"))
55 (defvar org-babel-default-header-args:haskell
56 '((:padlines . "no")))
58 (defvar org-babel-haskell-lhs2tex-command "lhs2tex")
60 (defvar org-babel-haskell-eoe "\"org-babel-haskell-eoe\"")
62 (defun org-babel-execute:haskell (body params)
63 "Execute a block of Haskell code."
64 (let* ((session (cdr (assoc :session params)))
65 (vars (mapcar #'cdr (org-babel-get-header params :var)))
66 (result-type (cdr (assoc :result-type params)))
67 (full-body (org-babel-expand-body:generic
68 body params
69 (org-babel-variable-assignments:haskell params)))
70 (session (org-babel-haskell-initiate-session session params))
71 (raw (org-babel-comint-with-output
72 (session org-babel-haskell-eoe t full-body)
73 (insert (org-babel-trim full-body))
74 (comint-send-input nil t)
75 (insert org-babel-haskell-eoe)
76 (comint-send-input nil t)))
77 (results (mapcar
78 #'org-babel-haskell-read-string
79 (cdr (member org-babel-haskell-eoe
80 (reverse (mapcar #'org-babel-trim raw)))))))
81 (org-babel-reassemble-table
82 (let ((result
83 (case result-type
84 (output (mapconcat #'identity (reverse (cdr results)) "\n"))
85 (value (car results)))))
86 (org-babel-result-cond (cdr (assoc :result-params params))
87 result (org-babel-haskell-table-or-string result)))
88 (org-babel-pick-name (cdr (assoc :colname-names params))
89 (cdr (assoc :colname-names params)))
90 (org-babel-pick-name (cdr (assoc :rowname-names params))
91 (cdr (assoc :rowname-names params))))))
93 (defun org-babel-haskell-read-string (string)
94 "Strip \\\"s from around a haskell string."
95 (if (string-match "^\"\\([^\000]+\\)\"$" string)
96 (match-string 1 string)
97 string))
99 (defun org-babel-haskell-initiate-session (&optional session params)
100 "Initiate a haskell session.
101 If there is not a current inferior-process-buffer in SESSION
102 then create one. Return the initialized session."
103 (require 'inf-haskell)
104 (or (get-buffer "*haskell*")
105 (save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
107 (defun org-babel-load-session:haskell (session body params)
108 "Load BODY into SESSION."
109 (save-window-excursion
110 (let* ((buffer (org-babel-prep-session:haskell session params))
111 (load-file (concat (org-babel-temp-file "haskell-load-") ".hs")))
112 (with-temp-buffer
113 (insert body) (write-file load-file)
114 (haskell-mode) (inferior-haskell-load-file))
115 buffer)))
117 (defun org-babel-prep-session:haskell (session params)
118 "Prepare SESSION according to the header arguments in PARAMS."
119 (save-window-excursion
120 (let ((buffer (org-babel-haskell-initiate-session session)))
121 (org-babel-comint-in-buffer buffer
122 (mapc (lambda (line)
123 (insert line)
124 (comint-send-input nil t))
125 (org-babel-variable-assignments:haskell params)))
126 (current-buffer))))
128 (defun org-babel-variable-assignments:haskell (params)
129 "Return list of haskell statements assigning the block's variables."
130 (mapcar (lambda (pair)
131 (format "let %s = %s"
132 (car pair)
133 (org-babel-haskell-var-to-haskell (cdr pair))))
134 (mapcar #'cdr (org-babel-get-header params :var))))
136 (defun org-babel-haskell-table-or-string (results)
137 "Convert RESULTS to an Emacs-lisp table or string.
138 If RESULTS look like a table, then convert them into an
139 Emacs-lisp table, otherwise return the results as a string."
140 (org-babel-script-escape results))
142 (defun org-babel-haskell-var-to-haskell (var)
143 "Convert an elisp value VAR into a haskell variable.
144 The elisp VAR is converted to a string of haskell source code
145 specifying a variable of the same value."
146 (if (listp var)
147 (concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var ", ") "]")
148 (format "%S" var)))
150 (defvar org-export-copy-to-kill-ring)
151 (declare-function org-export-to-file "ox"
152 (backend file
153 &optional async subtreep visible-only body-only ext-plist))
154 (defun org-babel-haskell-export-to-lhs (&optional arg)
155 "Export to a .lhs file with all haskell code blocks escaped.
156 When called with a prefix argument the resulting
157 .lhs file will be exported to a .tex file. This function will
158 create two new files, base-name.lhs and base-name.tex where
159 base-name is the name of the current org-mode file.
161 Note that all standard Babel literate programming
162 constructs (header arguments, no-web syntax etc...) are ignored."
163 (interactive "P")
164 (let* ((contents (buffer-string))
165 (haskell-regexp
166 (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
167 "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
168 (base-name (file-name-sans-extension (buffer-file-name)))
169 (tmp-file (org-babel-temp-file "haskell-"))
170 (tmp-org-file (concat tmp-file ".org"))
171 (tmp-tex-file (concat tmp-file ".tex"))
172 (lhs-file (concat base-name ".lhs"))
173 (tex-file (concat base-name ".tex"))
174 (command (concat org-babel-haskell-lhs2tex-command
175 " " (org-babel-process-file-name lhs-file)
176 " > " (org-babel-process-file-name tex-file)))
177 (preserve-indentp org-src-preserve-indentation)
178 indentation)
179 ;; escape haskell source-code blocks
180 (with-temp-file tmp-org-file
181 (insert contents)
182 (goto-char (point-min))
183 (while (re-search-forward haskell-regexp nil t)
184 (save-match-data (setq indentation (length (match-string 1))))
185 (replace-match (save-match-data
186 (concat
187 "#+begin_latex\n\\begin{code}\n"
188 (if (or preserve-indentp
189 (string-match "-i" (match-string 2)))
190 (match-string 3)
191 (org-remove-indentation (match-string 3)))
192 "\n\\end{code}\n#+end_latex\n"))
193 t t)
194 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
195 (save-excursion
196 ;; export to latex w/org and save as .lhs
197 (require 'ox-latex)
198 (find-file tmp-org-file)
199 ;; Ensure we do not clutter kill ring with incomplete results.
200 (let (org-export-copy-to-kill-ring)
201 (org-export-to-file 'latex tmp-tex-file))
202 (kill-buffer nil)
203 (delete-file tmp-org-file)
204 (find-file tmp-tex-file)
205 (goto-char (point-min)) (forward-line 2)
206 (insert "%include polycode.fmt\n")
207 ;; ensure all \begin/end{code} statements start at the first column
208 (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t)
209 (replace-match (save-match-data (org-remove-indentation (match-string 0)))
210 t t))
211 (setq contents (buffer-string))
212 (save-buffer) (kill-buffer nil))
213 (delete-file tmp-tex-file)
214 ;; save org exported latex to a .lhs file
215 (with-temp-file lhs-file (insert contents))
216 (if (not arg)
217 (find-file lhs-file)
218 ;; process .lhs file with lhs2tex
219 (message "running %s" command) (shell-command command) (find-file tex-file))))
221 (provide 'ob-haskell)
225 ;;; ob-haskell.el ends here