1 ;;; org-babel-haskell.el --- org-babel functions for haskell evaluation
3 ;; Copyright (C) 2009 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
12 ;; This program 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, or (at your option)
17 ;; This program 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Org-Babel support for evaluating haskell source code. This one will
30 ;; be sort of tricky because haskell programs must be compiled before
31 ;; they can be run, but haskell code can also be run through an
32 ;; interactive interpreter.
34 ;; For now lets only allow evaluation using the haskell interpreter.
38 ;; - haskell-mode :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
40 ;; - inf-haskell :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
42 ;; - (optionally) lhs2tex :: http://people.cs.uu.nl/andres/lhs2tex/
46 (require 'haskell-mode
)
47 (require 'inf-haskell
)
49 (org-babel-add-interpreter "haskell")
51 (add-to-list 'org-babel-tangle-langs
'("haskell" "hs"))
53 (defvar org-babel-haskell-lhs2tex-command
"lhs2tex")
55 (defvar org-babel-haskell-eoe
"\"org-babel-haskell-eoe\"")
57 (defun org-babel-execute:haskell
(body params
)
58 "Execute a block of Haskell code with org-babel."
59 (message "executing haskell source code block")
60 (let* ((processed-params (org-babel-process-params params
))
61 (session (first processed-params
))
62 (vars (second processed-params
))
63 (result-type (fourth processed-params
))
66 (lambda (pair) (format "let %s = %s;" (car pair
) (cdr pair
)))
67 vars
"\n") "\n" body
"\n"))
68 (session (org-babel-prep-session:haskell session params
))
69 (raw (org-babel-comint-with-output session org-babel-haskell-eoe t
70 (insert (org-babel-trim full-body
))
71 (comint-send-input nil t
)
72 (insert org-babel-haskell-eoe
)
73 (comint-send-input nil t
)))
75 #'org-babel-haskell-read-string
76 (cdr (member org-babel-haskell-eoe
77 (reverse (mapcar #'org-babel-trim raw
)))))))
79 (output (mapconcat #'identity
(reverse (cdr results
)) "\n"))
80 (value (org-babel-haskell-table-or-string (car results
))))))
82 (defun org-babel-haskell-read-string (string)
83 "Strip \\\"s from around haskell string"
84 (if (string-match "\"\\([^\000]+\\)\"" string
)
85 (match-string 1 string
)
88 (defun org-babel-haskell-initiate-session (&optional session
)
89 "If there is not a current inferior-process-buffer in SESSION
90 then create. Return the initialized session."
91 ;; TODO: make it possible to have multiple sessions
92 (run-haskell) (current-buffer))
94 (defun org-babel-load-session:haskell
(session body params
)
95 "Load BODY into SESSION."
96 (save-window-excursion
97 (let* ((buffer (org-babel-prep-session:haskell session params
))
98 (load-file (concat (make-temp-file "org-babel-haskell-load") ".hs")))
100 (insert body
) (write-file load-file
)
101 (haskell-mode) (inferior-haskell-load-file))
104 (defun org-babel-prep-session:haskell
(session params
)
105 "Prepare SESSION according to the header arguments specified in PARAMS."
106 (save-window-excursion
107 (org-babel-haskell-initiate-session session
)
108 (let* ((vars (org-babel-ref-variables params
))
109 (var-lines (mapconcat ;; define any variables
113 (org-babel-ruby-var-to-ruby (cdr pair
))))
115 (vars-file (concat (make-temp-file "org-babel-haskell-vars") ".hs")))
118 (insert var-lines
) (write-file vars-file
)
119 (haskell-mode) (inferior-haskell-load-file)))
122 (defun org-babel-haskell-table-or-string (results)
123 "If the results look like a table, then convert them into an
124 Emacs-lisp table, otherwise return the results as a string."
126 (if (and (stringp results
) (string-match "^\\[.+\\]$" results
))
128 (replace-regexp-in-string
129 "\\[" "(" (replace-regexp-in-string
130 "\\]" ")" (replace-regexp-in-string
131 "," " " (replace-regexp-in-string
132 "'" "\"" results
)))))
135 (defun org-babel-haskell-export-to-lhs (&optional arg
)
136 "Export to a .lhs with all haskell code blocks escaped
137 appropriately. When called with a prefix argument the resulting
138 .lhs file will be exported to a .tex file. This function will
139 create two new files, base-name.lhs and base-name.tex where
140 base-name is the name of the current org-mode file.
142 Note that all standard org-babel literate programming
143 constructs (header arguments, no-web syntax etc...) are ignored."
145 (let* ((contents (buffer-string))
147 (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
148 "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
149 (base-name (file-name-sans-extension (buffer-file-name)))
150 (tmp-file (make-temp-file "ob-haskell"))
151 (tmp-org-file (concat tmp-file
".org"))
152 (tmp-tex-file (concat tmp-file
".tex"))
153 (lhs-file (concat base-name
".lhs"))
154 (tex-file (concat base-name
".tex"))
155 (command (concat org-babel-haskell-lhs2tex-command
" " lhs-file
" > " tex-file
))
156 (preserve-indentp org-src-preserve-indentation
)
158 ;; escape haskell source-code blocks
159 (with-temp-file tmp-org-file
161 (goto-char (point-min))
162 (while (re-search-forward haskell-regexp nil t
)
163 (save-match-data (setq indentation
(length (match-string 1))))
164 (replace-match (save-match-data
166 "#+begin_latex\n\\begin{code}\n"
167 (if (or preserve-indentp
168 (string-match "-i" (match-string 2)))
170 (org-remove-indentation (match-string 3)))
171 "\n\\end{code}\n#+end_latex\n"))
173 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation
)))
175 ;; export to latex w/org and save as .lhs
176 (find-file tmp-org-file
) (funcall 'org-export-as-latex nil
)
178 (delete-file tmp-org-file
)
179 (find-file tmp-tex-file
)
180 (goto-char (point-min)) (forward-line 2)
181 (insert "%include polycode.fmt\n")
182 ;; ensure all \begin/end{code} statements start at the first column
183 (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t
)
184 (replace-match (save-match-data (org-remove-indentation (match-string 0)))
186 (setq contents
(buffer-string))
187 (save-buffer) (kill-buffer))
188 (delete-file tmp-tex-file
)
189 ;; save org exported latex to a .lhs file
190 (with-temp-file lhs-file
(insert contents
))
193 ;; process .lhs file with lhs2tex
194 (message "running %s" command
) (shell-command command
) (find-file tex-file
))))
196 (provide 'org-babel-haskell
)
197 ;;; org-babel-haskell.el ends here