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/
44 (require 'haskell-mode
)
45 (require 'inf-haskell
)
47 (add-to-list 'org-babel-tangle-lang-exts
'("haskell" .
"hs"))
49 (defvar org-babel-default-header-args
:haskell
'())
51 (defvar org-babel-haskell-lhs2tex-command
"lhs2tex")
53 (defvar org-babel-haskell-eoe
"\"org-babel-haskell-eoe\"")
55 (defun org-babel-expand-body:haskell
(body params
&optional processed-params
)
56 "Expand BODY according to PARAMS, return the expanded body."
57 (let (vars (nth 1 (or processed-params
(org-babel-process-params params
))))
60 (lambda (pair) (format "let %s = %s;" (car pair
) (cdr pair
)))
61 vars
"\n") "\n" body
"\n")))
63 (defun org-babel-execute:haskell
(body params
)
64 "Execute a block of Haskell code with org-babel."
65 (message "executing haskell source code block")
66 (let* ((processed-params (org-babel-process-params params
))
67 (session (first processed-params
))
68 (vars (nth 1 processed-params
))
69 (result-type (nth 3 processed-params
))
70 (full-body (org-babel-expand-body:haskell body params processed-params
))
71 (session (org-babel-prep-session:haskell 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
)))
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
84 (output (mapconcat #'identity
(reverse (cdr results
)) "\n"))
85 (value (org-babel-haskell-table-or-string (car results
))))
86 (org-babel-pick-name (nth 4 processed-params
) (cdr (assoc :colnames params
)))
87 (org-babel-pick-name (nth 5 processed-params
) (cdr (assoc :rownames params
))))))
89 (defun org-babel-haskell-read-string (string)
90 "Strip \\\"s from around a haskell string."
91 (if (string-match "^\"\\([^\000]+\\)\"$" string
)
92 (match-string 1 string
)
95 (defun org-babel-haskell-initiate-session (&optional session params
)
96 "If there is not a current inferior-process-buffer in SESSION
97 then create one. Return the initialized session."
98 ;; TODO: make it possible to have multiple sessions
99 (run-haskell) (current-buffer))
101 (defun org-babel-load-session:haskell
(session body params
)
102 "Load BODY into SESSION."
103 (save-window-excursion
104 (let* ((buffer (org-babel-prep-session:haskell session params
))
105 (load-file (concat (make-temp-file "org-babel-haskell-load") ".hs")))
107 (insert body
) (write-file load-file
)
108 (haskell-mode) (inferior-haskell-load-file))
111 (defun org-babel-prep-session:haskell
(session params
)
112 "Prepare SESSION according to the header arguments specified in PARAMS."
113 (save-window-excursion
114 (org-babel-haskell-initiate-session session
)
115 (let* ((vars (org-babel-ref-variables params
))
116 (var-lines (mapconcat ;; define any variables
120 (org-babel-ruby-var-to-ruby (cdr pair
))))
122 (vars-file (concat (make-temp-file "org-babel-haskell-vars") ".hs")))
125 (insert var-lines
) (write-file vars-file
)
126 (haskell-mode) (inferior-haskell-load-file)))
129 (defun org-babel-haskell-table-or-string (results)
130 "If the results look like a table, then convert them into an
131 Emacs-lisp table, otherwise return the results as a string."
133 (if (and (stringp results
) (string-match "^\\[.+\\]$" results
))
136 (replace-regexp-in-string
137 "\\[" "(" (replace-regexp-in-string
138 "\\]" ")" (replace-regexp-in-string
139 "," " " (replace-regexp-in-string
140 "'" "\"" results
))))))
143 (defun org-babel-haskell-export-to-lhs (&optional arg
)
144 "Export to a .lhs file with all haskell code blocks escaped
145 appropriately. When called with a prefix argument the resulting
146 .lhs file will be exported to a .tex file. This function will
147 create two new files, base-name.lhs and base-name.tex where
148 base-name is the name of the current org-mode file.
150 Note that all standard org-babel literate programming
151 constructs (header arguments, no-web syntax etc...) are ignored."
153 (let* ((contents (buffer-string))
155 (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
156 "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
157 (base-name (file-name-sans-extension (buffer-file-name)))
158 (tmp-file (make-temp-file "ob-haskell"))
159 (tmp-org-file (concat tmp-file
".org"))
160 (tmp-tex-file (concat tmp-file
".tex"))
161 (lhs-file (concat base-name
".lhs"))
162 (tex-file (concat base-name
".tex"))
163 (command (concat org-babel-haskell-lhs2tex-command
" " lhs-file
" > " tex-file
))
164 (preserve-indentp org-src-preserve-indentation
)
166 ;; escape haskell source-code blocks
167 (with-temp-file tmp-org-file
169 (goto-char (point-min))
170 (while (re-search-forward haskell-regexp nil t
)
171 (save-match-data (setq indentation
(length (match-string 1))))
172 (replace-match (save-match-data
174 "#+begin_latex\n\\begin{code}\n"
175 (if (or preserve-indentp
176 (string-match "-i" (match-string 2)))
178 (org-remove-indentation (match-string 3)))
179 "\n\\end{code}\n#+end_latex\n"))
181 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation
)))
183 ;; export to latex w/org and save as .lhs
184 (find-file tmp-org-file
) (funcall 'org-export-as-latex nil
)
186 (delete-file tmp-org-file
)
187 (find-file tmp-tex-file
)
188 (goto-char (point-min)) (forward-line 2)
189 (insert "%include polycode.fmt\n")
190 ;; ensure all \begin/end{code} statements start at the first column
191 (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t
)
192 (replace-match (save-match-data (org-remove-indentation (match-string 0)))
194 (setq contents
(buffer-string))
195 (save-buffer) (kill-buffer))
196 (delete-file tmp-tex-file
)
197 ;; save org exported latex to a .lhs file
198 (with-temp-file lhs-file
(insert contents
))
201 ;; process .lhs file with lhs2tex
202 (message "running %s" command
) (shell-command command
) (find-file tex-file
))))
204 (provide 'ob-haskell
)
206 ;; arch-tag: b53f75f3-ba1a-4b05-82d9-a2a0d4e70804
208 ;;; ob-haskell.el ends here