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