lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / ob-haskell.el
blob0346c2d47aee7e7f3a7f493221a3baa06beb9dd2
1 ;;; ob-haskell.el --- Babel Functions for Haskell -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2019 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: https://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 <https://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 'org-macs)
44 (require 'comint)
46 (declare-function haskell-mode "ext:haskell-mode" ())
47 (declare-function run-haskell "ext:inf-haskell" (&optional arg))
48 (declare-function inferior-haskell-load-file
49 "ext:inf-haskell" (&optional reload))
51 (defvar org-babel-tangle-lang-exts)
52 (add-to-list 'org-babel-tangle-lang-exts '("haskell" . "hs"))
54 (defvar org-babel-default-header-args:haskell
55 '((:padlines . "no")))
57 (defvar org-babel-haskell-lhs2tex-command "lhs2tex")
59 (defvar org-babel-haskell-eoe "\"org-babel-haskell-eoe\"")
61 (defvar haskell-prompt-regexp)
63 (defun org-babel-execute:haskell (body params)
64 "Execute a block of Haskell code."
65 (require 'inf-haskell)
66 (add-hook 'inferior-haskell-hook
67 (lambda ()
68 (setq-local comint-prompt-regexp
69 (concat haskell-prompt-regexp "\\|^λ?> "))))
70 (let* ((session (cdr (assq :session params)))
71 (result-type (cdr (assq :result-type params)))
72 (full-body (org-babel-expand-body:generic
73 body params
74 (org-babel-variable-assignments:haskell params)))
75 (session (org-babel-haskell-initiate-session session params))
76 (comint-preoutput-filter-functions
77 (cons 'ansi-color-filter-apply comint-preoutput-filter-functions))
78 (raw (org-babel-comint-with-output
79 (session org-babel-haskell-eoe t full-body)
80 (insert (org-trim full-body))
81 (comint-send-input nil t)
82 (insert org-babel-haskell-eoe)
83 (comint-send-input nil t)))
84 (results (mapcar #'org-strip-quotes
85 (cdr (member org-babel-haskell-eoe
86 (reverse (mapcar #'org-trim raw)))))))
87 (org-babel-reassemble-table
88 (let ((result
89 (pcase result-type
90 (`output (mapconcat #'identity (reverse (cdr results)) "\n"))
91 (`value (car results)))))
92 (org-babel-result-cond (cdr (assq :result-params params))
93 result (org-babel-script-escape result)))
94 (org-babel-pick-name (cdr (assq :colname-names params))
95 (cdr (assq :colname-names params)))
96 (org-babel-pick-name (cdr (assq :rowname-names params))
97 (cdr (assq :rowname-names params))))))
99 (defun org-babel-haskell-initiate-session (&optional _session _params)
100 "Initiate a haskell session.
101 If there is not a current inferior-process-buffer in SESSION
102 then create one. Return the initialized session."
103 (require 'inf-haskell)
104 (or (get-buffer "*haskell*")
105 (save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
107 (defun org-babel-load-session:haskell (session body params)
108 "Load BODY into SESSION."
109 (save-window-excursion
110 (let* ((buffer (org-babel-prep-session:haskell session params))
111 (load-file (concat (org-babel-temp-file "haskell-load-") ".hs")))
112 (with-temp-buffer
113 (insert body) (write-file load-file)
114 (haskell-mode) (inferior-haskell-load-file))
115 buffer)))
117 (defun org-babel-prep-session:haskell (session params)
118 "Prepare SESSION according to the header arguments in PARAMS."
119 (save-window-excursion
120 (let ((buffer (org-babel-haskell-initiate-session session)))
121 (org-babel-comint-in-buffer buffer
122 (mapc (lambda (line)
123 (insert line)
124 (comint-send-input nil t))
125 (org-babel-variable-assignments:haskell params)))
126 (current-buffer))))
128 (defun org-babel-variable-assignments:haskell (params)
129 "Return list of haskell statements assigning the block's variables."
130 (mapcar (lambda (pair)
131 (format "let %s = %s"
132 (car pair)
133 (org-babel-haskell-var-to-haskell (cdr pair))))
134 (org-babel--get-vars params)))
136 (defun org-babel-haskell-var-to-haskell (var)
137 "Convert an elisp value VAR into a haskell variable.
138 The elisp VAR is converted to a string of haskell source code
139 specifying a variable of the same value."
140 (if (listp var)
141 (concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var ", ") "]")
142 (format "%S" var)))
144 (defvar org-export-copy-to-kill-ring)
145 (declare-function org-export-to-file "ox"
146 (backend file
147 &optional async subtreep visible-only body-only
148 ext-plist post-process))
149 (defun org-babel-haskell-export-to-lhs (&optional arg)
150 "Export to a .lhs file with all haskell code blocks escaped.
151 When called with a prefix argument the resulting
152 .lhs file will be exported to a .tex file. This function will
153 create two new files, base-name.lhs and base-name.tex where
154 base-name is the name of the current Org file.
156 Note that all standard Babel literate programming
157 constructs (header arguments, no-web syntax etc...) are ignored."
158 (interactive "P")
159 (let* ((contents (buffer-string))
160 (haskell-regexp
161 (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
162 "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
163 (base-name (file-name-sans-extension (buffer-file-name)))
164 (tmp-file (org-babel-temp-file "haskell-"))
165 (tmp-org-file (concat tmp-file ".org"))
166 (tmp-tex-file (concat tmp-file ".tex"))
167 (lhs-file (concat base-name ".lhs"))
168 (tex-file (concat base-name ".tex"))
169 (command (concat org-babel-haskell-lhs2tex-command
170 " " (org-babel-process-file-name lhs-file)
171 " > " (org-babel-process-file-name tex-file)))
172 (preserve-indentp org-src-preserve-indentation)
173 indentation)
174 ;; escape haskell source-code blocks
175 (with-temp-file tmp-org-file
176 (insert contents)
177 (goto-char (point-min))
178 (while (re-search-forward haskell-regexp nil t)
179 (save-match-data (setq indentation (length (match-string 1))))
180 (replace-match (save-match-data
181 (concat
182 "#+begin_export latex\n\\begin{code}\n"
183 (if (or preserve-indentp
184 (string-match "-i" (match-string 2)))
185 (match-string 3)
186 (org-remove-indentation (match-string 3)))
187 "\n\\end{code}\n#+end_export\n"))
188 t t)
189 (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
190 (save-excursion
191 ;; export to latex w/org and save as .lhs
192 (require 'ox-latex)
193 (find-file tmp-org-file)
194 ;; Ensure we do not clutter kill ring with incomplete results.
195 (let (org-export-copy-to-kill-ring)
196 (org-export-to-file 'latex tmp-tex-file))
197 (kill-buffer nil)
198 (delete-file tmp-org-file)
199 (find-file tmp-tex-file)
200 (goto-char (point-min)) (forward-line 2)
201 (insert "%include polycode.fmt\n")
202 ;; ensure all \begin/end{code} statements start at the first column
203 (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t)
204 (replace-match (save-match-data (org-remove-indentation (match-string 0)))
205 t t))
206 (setq contents (buffer-string))
207 (save-buffer) (kill-buffer nil))
208 (delete-file tmp-tex-file)
209 ;; save org exported latex to a .lhs file
210 (with-temp-file lhs-file (insert contents))
211 (if (not arg)
212 (find-file lhs-file)
213 ;; process .lhs file with lhs2tex
214 (message "running %s" command) (shell-command command) (find-file tex-file))))
216 (provide 'ob-haskell)
220 ;;; ob-haskell.el ends here