Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / ob-haskell.el
blobecce6dcd5df4af63ff024c041b4fcc643189dec9
1 ;;; ob-haskell.el --- Babel Functions for Haskell -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2017 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating haskell source code. This one will
27 ;; be sort of tricky because haskell programs must be compiled before
28 ;; they can be run, but haskell code can also be run through an
29 ;; interactive interpreter.
31 ;; For now lets only allow evaluation using the haskell interpreter.
33 ;;; Requirements:
35 ;; - haskell-mode :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
37 ;; - inf-haskell :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
39 ;; - (optionally) lhs2tex :: http://people.cs.uu.nl/andres/lhs2tex/
41 ;;; Code:
42 (require 'ob)
43 (require 'comint)
45 (declare-function org-remove-indentation "org" (code &optional n))
46 (declare-function org-trim "org" (s &optional keep-lead))
47 (declare-function haskell-mode "ext:haskell-mode" ())
48 (declare-function run-haskell "ext:inf-haskell" (&optional arg))
49 (declare-function inferior-haskell-load-file
50 "ext:inf-haskell" (&optional reload))
52 (defvar org-babel-tangle-lang-exts)
53 (add-to-list 'org-babel-tangle-lang-exts '("haskell" . "hs"))
55 (defvar org-babel-default-header-args:haskell
56 '((:padlines . "no")))
58 (defvar org-babel-haskell-lhs2tex-command "lhs2tex")
60 (defvar org-babel-haskell-eoe "\"org-babel-haskell-eoe\"")
62 (defun org-babel-execute:haskell (body params)
63 "Execute a block of Haskell code."
64 (let* ((session (cdr (assq :session params)))
65 (result-type (cdr (assq :result-type params)))
66 (full-body (org-babel-expand-body:generic
67 body params
68 (org-babel-variable-assignments:haskell params)))
69 (session (org-babel-haskell-initiate-session session params))
70 (raw (org-babel-comint-with-output
71 (session org-babel-haskell-eoe t full-body)
72 (insert (org-trim full-body))
73 (comint-send-input nil t)
74 (insert org-babel-haskell-eoe)
75 (comint-send-input nil t)))
76 (results (mapcar
77 #'org-babel-strip-quotes
78 (cdr (member org-babel-haskell-eoe
79 (reverse (mapcar #'org-trim raw)))))))
80 (org-babel-reassemble-table
81 (let ((result
82 (pcase result-type
83 (`output (mapconcat #'identity (reverse (cdr results)) "\n"))
84 (`value (car results)))))
85 (org-babel-result-cond (cdr (assq :result-params params))
86 result (org-babel-script-escape result)))
87 (org-babel-pick-name (cdr (assq :colname-names params))
88 (cdr (assq :colname-names params)))
89 (org-babel-pick-name (cdr (assq :rowname-names params))
90 (cdr (assq :rowname-names params))))))
92 (defun org-babel-haskell-initiate-session (&optional _session _params)
93 "Initiate a haskell session.
94 If there is not a current inferior-process-buffer in SESSION
95 then create one. Return the initialized session."
96 (require 'inf-haskell)
97 (or (get-buffer "*haskell*")
98 (save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
100 (defun org-babel-load-session:haskell (session body params)
101 "Load BODY into SESSION."
102 (save-window-excursion
103 (let* ((buffer (org-babel-prep-session:haskell session params))
104 (load-file (concat (org-babel-temp-file "haskell-load-") ".hs")))
105 (with-temp-buffer
106 (insert body) (write-file load-file)
107 (haskell-mode) (inferior-haskell-load-file))
108 buffer)))
110 (defun org-babel-prep-session:haskell (session params)
111 "Prepare SESSION according to the header arguments in PARAMS."
112 (save-window-excursion
113 (let ((buffer (org-babel-haskell-initiate-session session)))
114 (org-babel-comint-in-buffer buffer
115 (mapc (lambda (line)
116 (insert line)
117 (comint-send-input nil t))
118 (org-babel-variable-assignments:haskell params)))
119 (current-buffer))))
121 (defun org-babel-variable-assignments:haskell (params)
122 "Return list of haskell statements assigning the block's variables."
123 (mapcar (lambda (pair)
124 (format "let %s = %s"
125 (car pair)
126 (org-babel-haskell-var-to-haskell (cdr pair))))
127 (org-babel--get-vars params)))
129 (defun org-babel-haskell-var-to-haskell (var)
130 "Convert an elisp value VAR into a haskell variable.
131 The elisp VAR is converted to a string of haskell source code
132 specifying a variable of the same value."
133 (if (listp var)
134 (concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var ", ") "]")
135 (format "%S" var)))
137 (defvar org-export-copy-to-kill-ring)
138 (declare-function org-export-to-file "ox"
139 (backend file
140 &optional async subtreep visible-only body-only
141 ext-plist post-process))
142 (defun org-babel-haskell-export-to-lhs (&optional arg)
143 "Export to a .lhs file with all haskell code blocks escaped.
144 When called with a prefix argument the resulting
145 .lhs file will be exported to a .tex file. This function will
146 create two new files, base-name.lhs and base-name.tex where
147 base-name is the name of the current Org file.
149 Note that all standard Babel literate programming
150 constructs (header arguments, no-web syntax etc...) are ignored."
151 (interactive "P")
152 (let* ((contents (buffer-string))
153 (haskell-regexp
154 (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
155 "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
156 (base-name (file-name-sans-extension (buffer-file-name)))
157 (tmp-file (org-babel-temp-file "haskell-"))
158 (tmp-org-file (concat tmp-file ".org"))
159 (tmp-tex-file (concat tmp-file ".tex"))
160 (lhs-file (concat base-name ".lhs"))
161 (tex-file (concat base-name ".tex"))
162 (command (concat org-babel-haskell-lhs2tex-command
163 " " (org-babel-process-file-name lhs-file)
164 " > " (org-babel-process-file-name tex-file)))
165 (preserve-indentp org-src-preserve-indentation)
166 indentation)
167 ;; escape haskell source-code blocks
168 (with-temp-file tmp-org-file
169 (insert contents)
170 (goto-char (point-min))
171 (while (re-search-forward haskell-regexp nil t)
172 (save-match-data (setq indentation (length (match-string 1))))
173 (replace-match (save-match-data
174 (concat
175 "#+begin_export latex\n\\begin{code}\n"
176 (if (or preserve-indentp
177 (string-match "-i" (match-string 2)))
178 (match-string 3)
179 (org-remove-indentation (match-string 3)))
180 "\n\\end{code}\n#+end_export\n"))
181 t t)
182 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
183 (save-excursion
184 ;; export to latex w/org and save as .lhs
185 (require 'ox-latex)
186 (find-file tmp-org-file)
187 ;; Ensure we do not clutter kill ring with incomplete results.
188 (let (org-export-copy-to-kill-ring)
189 (org-export-to-file 'latex tmp-tex-file))
190 (kill-buffer nil)
191 (delete-file tmp-org-file)
192 (find-file tmp-tex-file)
193 (goto-char (point-min)) (forward-line 2)
194 (insert "%include polycode.fmt\n")
195 ;; ensure all \begin/end{code} statements start at the first column
196 (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t)
197 (replace-match (save-match-data (org-remove-indentation (match-string 0)))
198 t t))
199 (setq contents (buffer-string))
200 (save-buffer) (kill-buffer nil))
201 (delete-file tmp-tex-file)
202 ;; save org exported latex to a .lhs file
203 (with-temp-file lhs-file (insert contents))
204 (if (not arg)
205 (find-file lhs-file)
206 ;; process .lhs file with lhs2tex
207 (message "running %s" command) (shell-command command) (find-file tex-file))))
209 (provide 'ob-haskell)
213 ;;; ob-haskell.el ends here