org-babel: Haskell lhs export: ensure all \[begin|end]{code} statements start at...
[rgr-org-mode.git] / contrib / babel / lisp / langs / org-babel-haskell.el
blob25e297fa033b764aae90b05a264bbb70553a7ef4
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
8 ;; Version: 0.01
10 ;;; License:
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)
15 ;; any later version.
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.
27 ;;; Commentary:
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.
36 ;;; Requirements:
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/
44 ;;; Code:
45 (require 'org-babel)
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. This function
59 is called by `org-babel-execute-src-block' with the following
60 variables pre-set using `multiple-value-bind'.
62 (session vars result-params result-type)"
63 (message "executing haskell source code block")
64 (let* ((full-body (concat
65 (mapconcat
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)))
74 (results (mapcar
75 #'org-babel-haskell-read-string
76 (cdr (member org-babel-haskell-eoe
77 (reverse (mapcar #'org-babel-trim raw)))))))
78 (case result-type
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)
86 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")))
99 (with-temp-buffer
100 (insert body) (write-file load-file)
101 (haskell-mode) (inferior-haskell-load-file))
102 buffer)))
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
110 (lambda (pair)
111 (format "%s=%s"
112 (car pair)
113 (org-babel-ruby-var-to-ruby (cdr pair))))
114 vars "\n"))
115 (vars-file (concat (make-temp-file "org-babel-haskell-vars") ".hs")))
116 (when vars
117 (with-temp-buffer
118 (insert var-lines) (write-file vars-file)
119 (haskell-mode) (inferior-haskell-load-file)))
120 (current-buffer))))
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."
125 (org-babel-read
126 (if (and (stringp results) (string-match "^\\[.+\\]$" results))
127 (org-babel-read
128 (replace-regexp-in-string
129 "\\[" "(" (replace-regexp-in-string
130 "\\]" ")" (replace-regexp-in-string
131 "," " " (replace-regexp-in-string
132 "'" "\"" results)))))
133 results)))
135 (defun org-babel-haskell-export-to-lhs ()
136 "Export to a .lhs with all haskell code blocks escaped
137 appropriately, then process the resulting .lhs file using
138 lhs2tex. This function will create two new files, base-name.lhs
139 and base-name.tex where base-name is the name of the current
140 org-mode file.
142 Note that all standard org-babel literate programming
143 constructs (header arguments, no-web syntax etc...) are ignored."
144 (interactive)
145 (let* ((contents (buffer-string))
146 (haskell-regexp
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 indentation)
157 ;; escape haskell source-code blocks
158 (with-temp-file tmp-org-file
159 (insert contents)
160 (goto-char (point-min))
161 (while (re-search-forward haskell-regexp nil t)
162 (save-match-data (setq indentation (length (match-string 1))))
163 (replace-match (save-match-data (concat
164 "#+begin_latex\n\\begin{code}\n"
165 (org-remove-indentation (match-string 3))
166 "\n\\end{code}\n#+end_latex\n"))
167 t t)
168 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
169 (save-excursion
170 ;; export to latex w/org and save as .lhs
171 (find-file tmp-org-file) (call-interactively 'org-export-as-latex)
172 (kill-buffer)
173 (delete-file tmp-org-file)
174 (find-file tmp-tex-file)
175 (goto-char (point-min)) (forward-line 2)
176 (insert "%include polycode.fmt\n")
177 ;; ensure all \begin/end{code} statements start at the first column
178 (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t)
179 (replace-match (save-match-data (org-remove-indentation (match-string 0)))
180 t t))
181 (setq contents (buffer-string))
182 (save-buffer) (kill-buffer))
183 (delete-file tmp-tex-file)
184 ;; save org exported latex to a .lhs file
185 (with-temp-file lhs-file (insert contents))
186 ;; process .lhs file with lhs2tex
187 (message "running %s" command) (shell-command command)))
189 (provide 'org-babel-haskell)
190 ;;; org-babel-haskell.el ends here