1 ;;; ob-haskell.el --- org-babel functions for haskell evaluation
3 ;; Copyright (C) 2009-2013 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/>.
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.
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/
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
'())
57 (defvar org-babel-haskell-lhs2tex-command
"lhs2tex")
59 (defvar org-babel-haskell-eoe
"\"org-babel-haskell-eoe\"")
61 (defun org-babel-execute:haskell
(body params
)
62 "Execute a block of Haskell code."
63 (let* ((session (cdr (assoc :session params
)))
64 (vars (mapcar #'cdr
(org-babel-get-header params
:var
)))
65 (result-type (cdr (assoc :result-type params
)))
66 (full-body (org-babel-expand-body:generic
68 (org-babel-variable-assignments:haskell params
)))
69 (session (org-babel-haskell-initiate-session session params
))
70 (raw (org-babel-comint-with-output
71 (session org-babel-haskell-eoe t full-body
)
72 (insert (org-babel-trim full-body
))
73 (comint-send-input nil t
)
74 (insert org-babel-haskell-eoe
)
75 (comint-send-input nil t
)))
77 #'org-babel-haskell-read-string
78 (cdr (member org-babel-haskell-eoe
79 (reverse (mapcar #'org-babel-trim raw
)))))))
80 (org-babel-reassemble-table
82 ((equal result-type
'output
)
83 (mapconcat #'identity
(reverse (cdr results
)) "\n"))
84 ((equal result-type
'value
)
85 (org-babel-haskell-table-or-string (car results
))))
86 (org-babel-pick-name (cdr (assoc :colname-names params
))
87 (cdr (assoc :colname-names params
)))
88 (org-babel-pick-name (cdr (assoc :rowname-names params
))
89 (cdr (assoc :rowname-names params
))))))
91 (defun org-babel-haskell-read-string (string)
92 "Strip \\\"s from around a haskell string."
93 (if (string-match "^\"\\([^\000]+\\)\"$" string
)
94 (match-string 1 string
)
97 (defun org-babel-haskell-initiate-session (&optional session params
)
98 "Initiate a haskell session.
99 If there is not a current inferior-process-buffer in SESSION
100 then create one. Return the initialized session."
101 (require 'inf-haskell
)
102 (or (get-buffer "*haskell*")
103 (save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
105 (defun org-babel-load-session:haskell
(session body params
)
106 "Load BODY into SESSION."
107 (save-window-excursion
108 (let* ((buffer (org-babel-prep-session:haskell session params
))
109 (load-file (concat (org-babel-temp-file "haskell-load-") ".hs")))
111 (insert body
) (write-file load-file
)
112 (haskell-mode) (inferior-haskell-load-file))
115 (defun org-babel-prep-session:haskell
(session params
)
116 "Prepare SESSION according to the header arguments in PARAMS."
117 (save-window-excursion
118 (let ((buffer (org-babel-haskell-initiate-session session
)))
119 (org-babel-comint-in-buffer buffer
122 (comint-send-input nil t
))
123 (org-babel-variable-assignments:haskell params
)))
126 (defun org-babel-variable-assignments:haskell
(params)
127 "Return list of haskell statements assigning the block's variables."
128 (mapcar (lambda (pair)
129 (format "let %s = %s"
131 (org-babel-haskell-var-to-haskell (cdr pair
))))
132 (mapcar #'cdr
(org-babel-get-header params
:var
))))
134 (defun org-babel-haskell-table-or-string (results)
135 "Convert RESULTS to an Emacs-lisp table or string.
136 If RESULTS look like a table, then convert them into an
137 Emacs-lisp table, otherwise return the results as a string."
138 (org-babel-script-escape results
))
140 (defun org-babel-haskell-var-to-haskell (var)
141 "Convert an elisp value VAR into a haskell variable.
142 The elisp VAR is converted to a string of haskell source code
143 specifying a variable of the same value."
145 (concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var
", ") "]")
148 (defvar org-src-preserve-indentation
)
149 (declare-function org-export-to-file
"ox"
151 &optional subtreep visible-only body-only ext-plist
))
152 (defun org-babel-haskell-export-to-lhs (&optional arg
)
153 "Export to a .lhs file with all haskell code blocks escaped.
154 When called with a prefix argument the resulting
155 .lhs file will be exported to a .tex file. This function will
156 create two new files, base-name.lhs and base-name.tex where
157 base-name is the name of the current org-mode file.
159 Note that all standard Babel literate programming
160 constructs (header arguments, no-web syntax etc...) are ignored."
162 (let* ((contents (buffer-string))
164 (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
165 "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
166 (base-name (file-name-sans-extension (buffer-file-name)))
167 (tmp-file (org-babel-temp-file "haskell-"))
168 (tmp-org-file (concat tmp-file
".org"))
169 (tmp-tex-file (concat tmp-file
".tex"))
170 (lhs-file (concat base-name
".lhs"))
171 (tex-file (concat base-name
".tex"))
172 (command (concat org-babel-haskell-lhs2tex-command
173 " " (org-babel-process-file-name lhs-file
)
174 " > " (org-babel-process-file-name tex-file
)))
175 (preserve-indentp org-src-preserve-indentation
)
177 ;; escape haskell source-code blocks
178 (with-temp-file tmp-org-file
180 (goto-char (point-min))
181 (while (re-search-forward haskell-regexp nil t
)
182 (save-match-data (setq indentation
(length (match-string 1))))
183 (replace-match (save-match-data
185 "#+begin_latex\n\\begin{code}\n"
186 (if (or preserve-indentp
187 (string-match "-i" (match-string 2)))
189 (org-remove-indentation (match-string 3)))
190 "\n\\end{code}\n#+end_latex\n"))
192 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation
)))
194 ;; export to latex w/org and save as .lhs
196 (find-file tmp-org-file
)
197 ;; Ensure we do not clutter kill ring with incomplete results.
198 (let (org-export-copy-to-kill-ring)
199 (org-export-to-file 'latex tmp-tex-file
))
201 (delete-file tmp-org-file
)
202 (find-file tmp-tex-file
)
203 (goto-char (point-min)) (forward-line 2)
204 (insert "%include polycode.fmt\n")
205 ;; ensure all \begin/end{code} statements start at the first column
206 (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t
)
207 (replace-match (save-match-data (org-remove-indentation (match-string 0)))
209 (setq contents
(buffer-string))
210 (save-buffer) (kill-buffer nil
))
211 (delete-file tmp-tex-file
)
212 ;; save org exported latex to a .lhs file
213 (with-temp-file lhs-file
(insert contents
))
216 ;; process .lhs file with lhs2tex
217 (message "running %s" command
) (shell-command command
) (find-file tex-file
))))
219 (provide 'ob-haskell
)
223 ;;; ob-haskell.el ends here