Release 7.3
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-haskell.el
blob1ae8fba66b6933a701b934cbbf1af4086e01e439
1 ;;; ob-haskell.el --- org-babel functions for haskell evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.3
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
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.
32 ;; For now lets only allow evaluation using the haskell interpreter.
34 ;;; Requirements:
36 ;; - haskell-mode :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
38 ;; - inf-haskell :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
40 ;; - (optionally) lhs2tex :: http://people.cs.uu.nl/andres/lhs2tex/
42 ;;; Code:
43 (require 'ob)
44 (require 'ob-comint)
45 (require 'comint)
46 (eval-when-compile (require 'cl))
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))
54 (add-to-list 'org-babel-tangle-lang-exts '("haskell" . "hs"))
56 (defvar org-babel-default-header-args:haskell '())
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 (cond
83 ((equal result-type 'output)
84 (mapconcat #'identity (reverse (cdr results)) "\n"))
85 ((equal result-type 'value)
86 (org-babel-haskell-table-or-string (car results))))
87 (org-babel-pick-name (cdr (assoc :colname-names params))
88 (cdr (assoc :colname-names params)))
89 (org-babel-pick-name (cdr (assoc :rowname-names params))
90 (cdr (assoc :rowname-names params))))))
92 (defun org-babel-haskell-read-string (string)
93 "Strip \\\"s from around a haskell string."
94 (if (string-match "^\"\\([^\000]+\\)\"$" string)
95 (match-string 1 string)
96 string))
98 (defun org-babel-haskell-initiate-session (&optional session params)
99 "Initiate a haskell session.
100 If there is not a current inferior-process-buffer in SESSION
101 then create one. Return the initialized session."
102 (require 'inf-haskell)
103 (or (get-buffer "*haskell*")
104 (save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
106 (defun org-babel-load-session:haskell (session body params)
107 "Load BODY into SESSION."
108 (save-window-excursion
109 (let* ((buffer (org-babel-prep-session:haskell session params))
110 (load-file (concat (org-babel-temp-file "haskell-load-") ".hs")))
111 (with-temp-buffer
112 (insert body) (write-file load-file)
113 (haskell-mode) (inferior-haskell-load-file))
114 buffer)))
116 (defun org-babel-prep-session:haskell (session params)
117 "Prepare SESSION according to the header arguments in PARAMS."
118 (save-window-excursion
119 (let ((buffer (org-babel-haskell-initiate-session session)))
120 (org-babel-comint-in-buffer buffer
121 (mapc (lambda (line)
122 (insert line)
123 (comint-send-input nil t))
124 (org-babel-variable-assignments:haskell params)))
125 (current-buffer))))
127 (defun org-babel-variable-assignments:haskell (params)
128 "Return list of haskell statements assigning the block's variables"
129 (mapcar (lambda (pair)
130 (format "let %s = %s"
131 (car pair)
132 (org-babel-haskell-var-to-haskell (cdr pair))))
133 (mapcar #'cdr (org-babel-get-header params :var))))
135 (defun org-babel-haskell-table-or-string (results)
136 "Convert RESULTS to an Emacs-lisp table or string.
137 If RESULTS look like a table, then convert them into an
138 Emacs-lisp table, otherwise return the results as a string."
139 (org-babel-read
140 (if (and (stringp results) (string-match "^\\[.+\\]$" results))
141 (org-babel-read
142 (concat "'"
143 (replace-regexp-in-string
144 "\\[" "(" (replace-regexp-in-string
145 "\\]" ")" (replace-regexp-in-string
146 "," " " (replace-regexp-in-string
147 "'" "\"" results))))))
148 results)))
150 (defun org-babel-haskell-var-to-haskell (var)
151 "Convert an elisp value VAR into a haskell variable.
152 The elisp VAR is converted to a string of haskell source code
153 specifying a variable of the same value."
154 (if (listp var)
155 (concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var ", ") "]")
156 (format "%S" var)))
158 (defvar org-src-preserve-indentation)
159 (defun org-babel-haskell-export-to-lhs (&optional arg)
160 "Export to a .lhs file with all haskell code blocks escaped.
161 When called with a prefix argument the resulting
162 .lhs file will be exported to a .tex file. This function will
163 create two new files, base-name.lhs and base-name.tex where
164 base-name is the name of the current org-mode file.
166 Note that all standard Babel literate programming
167 constructs (header arguments, no-web syntax etc...) are ignored."
168 (interactive "P")
169 (let* ((contents (buffer-string))
170 (haskell-regexp
171 (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
172 "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
173 (base-name (file-name-sans-extension (buffer-file-name)))
174 (tmp-file (org-babel-temp-file "haskell-"))
175 (tmp-org-file (concat tmp-file ".org"))
176 (tmp-tex-file (concat tmp-file ".tex"))
177 (lhs-file (concat base-name ".lhs"))
178 (tex-file (concat base-name ".tex"))
179 (command (concat org-babel-haskell-lhs2tex-command
180 " " (org-babel-process-file-name lhs-file)
181 " > " (org-babel-process-file-name tex-file)))
182 (preserve-indentp org-src-preserve-indentation)
183 indentation)
184 ;; escape haskell source-code blocks
185 (with-temp-file tmp-org-file
186 (insert contents)
187 (goto-char (point-min))
188 (while (re-search-forward haskell-regexp nil t)
189 (save-match-data (setq indentation (length (match-string 1))))
190 (replace-match (save-match-data
191 (concat
192 "#+begin_latex\n\\begin{code}\n"
193 (if (or preserve-indentp
194 (string-match "-i" (match-string 2)))
195 (match-string 3)
196 (org-remove-indentation (match-string 3)))
197 "\n\\end{code}\n#+end_latex\n"))
198 t t)
199 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
200 (save-excursion
201 ;; export to latex w/org and save as .lhs
202 (find-file tmp-org-file) (funcall 'org-export-as-latex nil)
203 (kill-buffer)
204 (delete-file tmp-org-file)
205 (find-file tmp-tex-file)
206 (goto-char (point-min)) (forward-line 2)
207 (insert "%include polycode.fmt\n")
208 ;; ensure all \begin/end{code} statements start at the first column
209 (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t)
210 (replace-match (save-match-data (org-remove-indentation (match-string 0)))
211 t t))
212 (setq contents (buffer-string))
213 (save-buffer) (kill-buffer))
214 (delete-file tmp-tex-file)
215 ;; save org exported latex to a .lhs file
216 (with-temp-file lhs-file (insert contents))
217 (if (not arg)
218 (find-file lhs-file)
219 ;; process .lhs file with lhs2tex
220 (message "running %s" command) (shell-command command) (find-file tex-file))))
222 (provide 'ob-haskell)
224 ;; arch-tag: b53f75f3-ba1a-4b05-82d9-a2a0d4e70804
226 ;;; ob-haskell.el ends here