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
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/>.
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.
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/
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-expand-body:haskell
(body params
&optional processed-params
)
63 "Expand BODY according to PARAMS, return the expanded body."
64 (let ((vars (nth 1 (or processed-params
(org-babel-process-params params
)))))
67 (lambda (pair) (format "let %s = %s"
69 (org-babel-haskell-var-to-haskell (cdr pair
))))
70 vars
"\n") "\n" body
"\n")))
72 (defun org-babel-execute:haskell
(body params
)
73 "Execute a block of Haskell code."
74 (let* ((processed-params (org-babel-process-params params
))
75 (session (nth 0 processed-params
))
76 (vars (nth 1 processed-params
))
77 (result-type (nth 3 processed-params
))
78 (full-body (org-babel-expand-body:haskell body params processed-params
))
79 (session (org-babel-haskell-initiate-session session params
))
80 (raw (org-babel-comint-with-output
81 (session org-babel-haskell-eoe t full-body
)
82 (insert (org-babel-trim full-body
))
83 (comint-send-input nil t
)
84 (insert org-babel-haskell-eoe
)
85 (comint-send-input nil t
)))
87 #'org-babel-haskell-read-string
88 (cdr (member org-babel-haskell-eoe
89 (reverse (mapcar #'org-babel-trim raw
)))))))
90 (org-babel-reassemble-table
92 ((equal result-type
'output
)
93 (mapconcat #'identity
(reverse (cdr results
)) "\n"))
94 ((equal result-type
'value
)
95 (org-babel-haskell-table-or-string (car results
))))
96 (org-babel-pick-name (nth 4 processed-params
) (cdr (assoc :colnames params
)))
97 (org-babel-pick-name (nth 5 processed-params
) (cdr (assoc :rownames params
))))))
99 (defun org-babel-haskell-read-string (string)
100 "Strip \\\"s from around a haskell string."
101 (if (string-match "^\"\\([^\000]+\\)\"$" string
)
102 (match-string 1 string
)
105 (defun org-babel-haskell-initiate-session (&optional session params
)
106 "Initiate a haskell session.
107 If there is not a current inferior-process-buffer in SESSION
108 then create one. Return the initialized session."
109 (require 'inf-haskell
)
110 (or (get-buffer "*haskell*")
111 (save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
113 (defun org-babel-load-session:haskell
114 (session body params
&optional processed-params
)
115 "Load BODY into SESSION."
116 (save-window-excursion
117 (let* ((buffer (org-babel-prep-session:haskell
118 session params processed-params
))
119 (load-file (concat (make-temp-file "org-babel-haskell-load") ".hs")))
121 (insert body
) (write-file load-file
)
122 (haskell-mode) (inferior-haskell-load-file))
125 (defun org-babel-prep-session:haskell
126 (session params
&optional processed-params
)
127 "Prepare SESSION according to the header arguments in PARAMS."
128 (save-window-excursion
129 (let ((pp (or processed-params
(org-babel-process-params params
)))
130 (buffer (org-babel-haskell-initiate-session session
)))
131 (org-babel-comint-in-buffer buffer
134 (insert (format "let %s = %s"
136 (org-babel-haskell-var-to-haskell (cdr pair
))))
137 (comint-send-input nil t
))
141 (defun org-babel-haskell-table-or-string (results)
142 "Convert RESULTS to an Emacs-lisp table or string.
143 If RESULTS look like a table, then convert them into an
144 Emacs-lisp table, otherwise return the results as a string."
146 (if (and (stringp results
) (string-match "^\\[.+\\]$" results
))
149 (replace-regexp-in-string
150 "\\[" "(" (replace-regexp-in-string
151 "\\]" ")" (replace-regexp-in-string
152 "," " " (replace-regexp-in-string
153 "'" "\"" results
))))))
156 (defun org-babel-haskell-var-to-haskell (var)
157 "Convert an elisp value VAR into a haskell variable.
158 The elisp VAR is converted to a string of haskell source code
159 specifying a variable of the same value."
161 (concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var
", ") "]")
164 (defvar org-src-preserve-indentation
)
165 (defun org-babel-haskell-export-to-lhs (&optional arg
)
166 "Export to a .lhs file with all haskell code blocks escaped.
167 When called with a prefix argument the resulting
168 .lhs file will be exported to a .tex file. This function will
169 create two new files, base-name.lhs and base-name.tex where
170 base-name is the name of the current org-mode file.
172 Note that all standard Babel literate programming
173 constructs (header arguments, no-web syntax etc...) are ignored."
175 (let* ((contents (buffer-string))
177 (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
178 "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
179 (base-name (file-name-sans-extension (buffer-file-name)))
180 (tmp-file (make-temp-file "ob-haskell"))
181 (tmp-org-file (concat tmp-file
".org"))
182 (tmp-tex-file (concat tmp-file
".tex"))
183 (lhs-file (concat base-name
".lhs"))
184 (tex-file (concat base-name
".tex"))
185 (command (concat org-babel-haskell-lhs2tex-command
" " lhs-file
" > " tex-file
))
186 (preserve-indentp org-src-preserve-indentation
)
188 ;; escape haskell source-code blocks
189 (with-temp-file tmp-org-file
191 (goto-char (point-min))
192 (while (re-search-forward haskell-regexp nil t
)
193 (save-match-data (setq indentation
(length (match-string 1))))
194 (replace-match (save-match-data
196 "#+begin_latex\n\\begin{code}\n"
197 (if (or preserve-indentp
198 (string-match "-i" (match-string 2)))
200 (org-remove-indentation (match-string 3)))
201 "\n\\end{code}\n#+end_latex\n"))
203 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation
)))
205 ;; export to latex w/org and save as .lhs
206 (find-file tmp-org-file
) (funcall 'org-export-as-latex nil
)
208 (delete-file tmp-org-file
)
209 (find-file tmp-tex-file
)
210 (goto-char (point-min)) (forward-line 2)
211 (insert "%include polycode.fmt\n")
212 ;; ensure all \begin/end{code} statements start at the first column
213 (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t
)
214 (replace-match (save-match-data (org-remove-indentation (match-string 0)))
216 (setq contents
(buffer-string))
217 (save-buffer) (kill-buffer))
218 (delete-file tmp-tex-file
)
219 ;; save org exported latex to a .lhs file
220 (with-temp-file lhs-file
(insert contents
))
223 ;; process .lhs file with lhs2tex
224 (message "running %s" command
) (shell-command command
) (find-file tex-file
))))
226 (provide 'ob-haskell
)
228 ;; arch-tag: b53f75f3-ba1a-4b05-82d9-a2a0d4e70804
230 ;;; ob-haskell.el ends here