Fix Org ChangeLog entries and remove arch-tag.
[bpt/emacs.git] / lisp / org / ob-haskell.el
CommitLineData
86fbb8ca
CD
1;;; ob-haskell.el --- org-babel functions for haskell evaluation
2
3ab2c837 3;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
86fbb8ca
CD
4
5;; Author: Eric Schulte
6;; Keywords: literate programming, reproducible research
7;; Homepage: http://orgmode.org
3ab2c837 8;; Version: 7.7
86fbb8ca
CD
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26
27;; Org-Babel support for evaluating haskell source code. This one will
28;; be sort of tricky because haskell programs must be compiled before
29;; they can be run, but haskell code can also be run through an
30;; interactive interpreter.
31;;
32;; For now lets only allow evaluation using the haskell interpreter.
33
34;;; Requirements:
35
36;; - haskell-mode :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
37;;
38;; - inf-haskell :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
39;;
40;; - (optionally) lhs2tex :: http://people.cs.uu.nl/andres/lhs2tex/
41
42;;; Code:
43(require 'ob)
44(require 'ob-comint)
45(require 'comint)
46(eval-when-compile (require 'cl))
47
48(declare-function org-remove-indentation "org" (code &optional n))
49(declare-function haskell-mode "ext:haskell-mode" ())
50(declare-function run-haskell "ext:inf-haskell" (&optional arg))
51(declare-function inferior-haskell-load-file
52 "ext:inf-haskell" (&optional reload))
53
3ab2c837 54(defvar org-babel-tangle-lang-exts)
86fbb8ca
CD
55(add-to-list 'org-babel-tangle-lang-exts '("haskell" . "hs"))
56
57(defvar org-babel-default-header-args:haskell '())
58
59(defvar org-babel-haskell-lhs2tex-command "lhs2tex")
60
61(defvar org-babel-haskell-eoe "\"org-babel-haskell-eoe\"")
62
86fbb8ca
CD
63(defun org-babel-execute:haskell (body params)
64 "Execute a block of Haskell code."
afe98dfa
CD
65 (let* ((session (cdr (assoc :session params)))
66 (vars (mapcar #'cdr (org-babel-get-header params :var)))
67 (result-type (cdr (assoc :result-type params)))
68 (full-body (org-babel-expand-body:generic
69 body params
70 (org-babel-variable-assignments:haskell params)))
86fbb8ca
CD
71 (session (org-babel-haskell-initiate-session session params))
72 (raw (org-babel-comint-with-output
73 (session org-babel-haskell-eoe t full-body)
74 (insert (org-babel-trim full-body))
75 (comint-send-input nil t)
76 (insert org-babel-haskell-eoe)
77 (comint-send-input nil t)))
78 (results (mapcar
79 #'org-babel-haskell-read-string
80 (cdr (member org-babel-haskell-eoe
81 (reverse (mapcar #'org-babel-trim raw)))))))
82 (org-babel-reassemble-table
83 (cond
84 ((equal result-type 'output)
85 (mapconcat #'identity (reverse (cdr results)) "\n"))
86 ((equal result-type 'value)
87 (org-babel-haskell-table-or-string (car results))))
afe98dfa
CD
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))))))
86fbb8ca
CD
92
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))
98
99(defun org-babel-haskell-initiate-session (&optional session params)
100 "Initiate a haskell session.
101If there is not a current inferior-process-buffer in SESSION
102then 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))))
106
afe98dfa 107(defun org-babel-load-session:haskell (session body params)
86fbb8ca
CD
108 "Load BODY into SESSION."
109 (save-window-excursion
afe98dfa
CD
110 (let* ((buffer (org-babel-prep-session:haskell session params))
111 (load-file (concat (org-babel-temp-file "haskell-load-") ".hs")))
86fbb8ca
CD
112 (with-temp-buffer
113 (insert body) (write-file load-file)
114 (haskell-mode) (inferior-haskell-load-file))
115 buffer)))
116
afe98dfa 117(defun org-babel-prep-session:haskell (session params)
86fbb8ca
CD
118 "Prepare SESSION according to the header arguments in PARAMS."
119 (save-window-excursion
afe98dfa 120 (let ((buffer (org-babel-haskell-initiate-session session)))
86fbb8ca 121 (org-babel-comint-in-buffer buffer
afe98dfa
CD
122 (mapc (lambda (line)
123 (insert line)
124 (comint-send-input nil t))
125 (org-babel-variable-assignments:haskell params)))
86fbb8ca
CD
126 (current-buffer))))
127
afe98dfa
CD
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))))
135
86fbb8ca
CD
136(defun org-babel-haskell-table-or-string (results)
137 "Convert RESULTS to an Emacs-lisp table or string.
138If RESULTS look like a table, then convert them into an
139Emacs-lisp table, otherwise return the results as a string."
acedf35c 140 (org-babel-script-escape results))
86fbb8ca
CD
141
142(defun org-babel-haskell-var-to-haskell (var)
143 "Convert an elisp value VAR into a haskell variable.
144The elisp VAR is converted to a string of haskell source code
145specifying a variable of the same value."
146 (if (listp var)
147 (concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var ", ") "]")
148 (format "%S" var)))
149
150(defvar org-src-preserve-indentation)
151(defun org-babel-haskell-export-to-lhs (&optional arg)
152 "Export to a .lhs file with all haskell code blocks escaped.
153When called with a prefix argument the resulting
154.lhs file will be exported to a .tex file. This function will
155create two new files, base-name.lhs and base-name.tex where
156base-name is the name of the current org-mode file.
157
158Note that all standard Babel literate programming
159constructs (header arguments, no-web syntax etc...) are ignored."
160 (interactive "P")
161 (let* ((contents (buffer-string))
162 (haskell-regexp
163 (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
164 "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
165 (base-name (file-name-sans-extension (buffer-file-name)))
afe98dfa 166 (tmp-file (org-babel-temp-file "haskell-"))
86fbb8ca
CD
167 (tmp-org-file (concat tmp-file ".org"))
168 (tmp-tex-file (concat tmp-file ".tex"))
169 (lhs-file (concat base-name ".lhs"))
170 (tex-file (concat base-name ".tex"))
afe98dfa
CD
171 (command (concat org-babel-haskell-lhs2tex-command
172 " " (org-babel-process-file-name lhs-file)
173 " > " (org-babel-process-file-name tex-file)))
86fbb8ca
CD
174 (preserve-indentp org-src-preserve-indentation)
175 indentation)
176 ;; escape haskell source-code blocks
177 (with-temp-file tmp-org-file
178 (insert contents)
179 (goto-char (point-min))
180 (while (re-search-forward haskell-regexp nil t)
181 (save-match-data (setq indentation (length (match-string 1))))
182 (replace-match (save-match-data
183 (concat
184 "#+begin_latex\n\\begin{code}\n"
185 (if (or preserve-indentp
186 (string-match "-i" (match-string 2)))
187 (match-string 3)
188 (org-remove-indentation (match-string 3)))
189 "\n\\end{code}\n#+end_latex\n"))
190 t t)
191 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
192 (save-excursion
193 ;; export to latex w/org and save as .lhs
194 (find-file tmp-org-file) (funcall 'org-export-as-latex nil)
3ab2c837 195 (kill-buffer nil)
86fbb8ca
CD
196 (delete-file tmp-org-file)
197 (find-file tmp-tex-file)
198 (goto-char (point-min)) (forward-line 2)
199 (insert "%include polycode.fmt\n")
200 ;; ensure all \begin/end{code} statements start at the first column
201 (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t)
202 (replace-match (save-match-data (org-remove-indentation (match-string 0)))
203 t t))
204 (setq contents (buffer-string))
3ab2c837 205 (save-buffer) (kill-buffer nil))
86fbb8ca
CD
206 (delete-file tmp-tex-file)
207 ;; save org exported latex to a .lhs file
208 (with-temp-file lhs-file (insert contents))
209 (if (not arg)
210 (find-file lhs-file)
211 ;; process .lhs file with lhs2tex
212 (message "running %s" command) (shell-command command) (find-file tex-file))))
213
214(provide 'ob-haskell)
215
5b409b39 216
86fbb8ca
CD
217
218;;; ob-haskell.el ends here