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 with org-babel."
74 (message "executing haskell source code block")
75 (let* ((processed-params (org-babel-process-params params
))
76 (session (nth 0 processed-params
))
77 (vars (nth 1 processed-params
))
78 (result-type (nth 3 processed-params
))
79 (full-body (org-babel-expand-body:haskell body params processed-params
))
80 (session (org-babel-haskell-initiate-session session params
))
81 (raw (org-babel-comint-with-output
82 (session org-babel-haskell-eoe t full-body
)
83 (insert (org-babel-trim full-body
))
84 (comint-send-input nil t
)
85 (insert org-babel-haskell-eoe
)
86 (comint-send-input nil t
)))
88 #'org-babel-haskell-read-string
89 (cdr (member org-babel-haskell-eoe
90 (reverse (mapcar #'org-babel-trim raw
)))))))
91 (org-babel-reassemble-table
93 ((equal result-type
'output
)
94 (mapconcat #'identity
(reverse (cdr results
)) "\n"))
95 ((equal result-type
'value
)
96 (org-babel-haskell-table-or-string (car results
))))
97 (org-babel-pick-name (nth 4 processed-params
) (cdr (assoc :colnames params
)))
98 (org-babel-pick-name (nth 5 processed-params
) (cdr (assoc :rownames params
))))))
100 (defun org-babel-haskell-read-string (string)
101 "Strip \\\"s from around a haskell string."
102 (if (string-match "^\"\\([^\000]+\\)\"$" string
)
103 (match-string 1 string
)
106 (defun org-babel-haskell-initiate-session (&optional session params
)
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 specified 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 "If the results look like a table, then convert them into an
143 Emacs-lisp table, otherwise return the results as a string."
145 (if (and (stringp results
) (string-match "^\\[.+\\]$" results
))
148 (replace-regexp-in-string
149 "\\[" "(" (replace-regexp-in-string
150 "\\]" ")" (replace-regexp-in-string
151 "," " " (replace-regexp-in-string
152 "'" "\"" results
))))))
155 (defun org-babel-haskell-var-to-haskell (var)
156 "Convert an elisp var into a string of haskell source code
157 specifying a var of the same value."
159 (concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var
", ") "]")
162 (defvar org-src-preserve-indentation
)
163 (defun org-babel-haskell-export-to-lhs (&optional arg
)
164 "Export to a .lhs file with all haskell code blocks escaped
165 appropriately. When called with a prefix argument the resulting
166 .lhs file will be exported to a .tex file. This function will
167 create two new files, base-name.lhs and base-name.tex where
168 base-name is the name of the current org-mode file.
170 Note that all standard org-babel literate programming
171 constructs (header arguments, no-web syntax etc...) are ignored."
173 (let* ((contents (buffer-string))
175 (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
176 "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
177 (base-name (file-name-sans-extension (buffer-file-name)))
178 (tmp-file (make-temp-file "ob-haskell"))
179 (tmp-org-file (concat tmp-file
".org"))
180 (tmp-tex-file (concat tmp-file
".tex"))
181 (lhs-file (concat base-name
".lhs"))
182 (tex-file (concat base-name
".tex"))
183 (command (concat org-babel-haskell-lhs2tex-command
" " lhs-file
" > " tex-file
))
184 (preserve-indentp org-src-preserve-indentation
)
186 ;; escape haskell source-code blocks
187 (with-temp-file tmp-org-file
189 (goto-char (point-min))
190 (while (re-search-forward haskell-regexp nil t
)
191 (save-match-data (setq indentation
(length (match-string 1))))
192 (replace-match (save-match-data
194 "#+begin_latex\n\\begin{code}\n"
195 (if (or preserve-indentp
196 (string-match "-i" (match-string 2)))
198 (org-remove-indentation (match-string 3)))
199 "\n\\end{code}\n#+end_latex\n"))
201 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation
)))
203 ;; export to latex w/org and save as .lhs
204 (find-file tmp-org-file
) (funcall 'org-export-as-latex nil
)
206 (delete-file tmp-org-file
)
207 (find-file tmp-tex-file
)
208 (goto-char (point-min)) (forward-line 2)
209 (insert "%include polycode.fmt\n")
210 ;; ensure all \begin/end{code} statements start at the first column
211 (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t
)
212 (replace-match (save-match-data (org-remove-indentation (match-string 0)))
214 (setq contents
(buffer-string))
215 (save-buffer) (kill-buffer))
216 (delete-file tmp-tex-file
)
217 ;; save org exported latex to a .lhs file
218 (with-temp-file lhs-file
(insert contents
))
221 ;; process .lhs file with lhs2tex
222 (message "running %s" command
) (shell-command command
) (find-file tex-file
))))
224 (provide 'ob-haskell
)
226 ;; arch-tag: b53f75f3-ba1a-4b05-82d9-a2a0d4e70804
228 ;;; ob-haskell.el ends here