ob-haskell.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. ;;; ob-haskell.el --- Babel Functions for Haskell -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2020 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: https://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org Babel support for evaluating Haskell source code.
  19. ;; Haskell programs must be compiled before
  20. ;; they can be run, but haskell code can also be run through an
  21. ;; interactive interpreter.
  22. ;;
  23. ;; By default we evaluate using the Haskell interpreter.
  24. ;; To use the compiler, specify :compile yes in the header.
  25. ;;; Requirements:
  26. ;; - haskell-mode: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
  27. ;; - inf-haskell: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
  28. ;; - (optionally) lhs2tex: http://people.cs.uu.nl/andres/lhs2tex/
  29. ;;; Code:
  30. (require 'ob)
  31. (require 'org-macs)
  32. (require 'comint)
  33. (declare-function haskell-mode "ext:haskell-mode" ())
  34. (declare-function run-haskell "ext:inf-haskell" (&optional arg))
  35. (declare-function inferior-haskell-load-file
  36. "ext:inf-haskell" (&optional reload))
  37. (declare-function org-entry-get "org" (pom property &optional inherit literal-nil))
  38. (defvar org-babel-tangle-lang-exts)
  39. (add-to-list 'org-babel-tangle-lang-exts '("haskell" . "hs"))
  40. (defvar org-babel-default-header-args:haskell
  41. '((:padlines . "no")))
  42. (defvar org-babel-haskell-lhs2tex-command "lhs2tex")
  43. (defvar org-babel-haskell-eoe "\"org-babel-haskell-eoe\"")
  44. (defvar haskell-prompt-regexp)
  45. (defcustom org-babel-haskell-compiler "ghc"
  46. "Command used to compile a Haskell source code file into an executable.
  47. May be either a command in the path, like \"ghc\" or an absolute
  48. path name, like \"/usr/local/bin/ghc\". The command can include
  49. a parameter, such as \"ghc -v\"."
  50. :group 'org-babel
  51. :package-version '(Org "9.4")
  52. :type 'string)
  53. (defconst org-babel-header-args:haskell '(compile . :any)
  54. "Haskell-specific header arguments.")
  55. (defun org-babel-haskell-execute (body params)
  56. "This function should only be called by `org-babel-execute:haskell'"
  57. (let* ((tmp-src-file (org-babel-temp-file "Haskell-src-" ".hs"))
  58. (tmp-bin-file
  59. (org-babel-process-file-name
  60. (org-babel-temp-file "Haskell-bin-" org-babel-exeext)))
  61. (cmdline (cdr (assq :cmdline params)))
  62. (cmdline (if cmdline (concat " " cmdline) ""))
  63. (flags (cdr (assq :flags params)))
  64. (flags (mapconcat #'identity
  65. (if (listp flags)
  66. flags
  67. (list flags))
  68. " "))
  69. (libs (org-babel-read
  70. (or (cdr (assq :libs params))
  71. (org-entry-get nil "libs" t))
  72. nil))
  73. (libs (mapconcat #'identity
  74. (if (listp libs) libs (list libs))
  75. " ")))
  76. (with-temp-file tmp-src-file (insert body))
  77. (org-babel-eval
  78. (format "%s -o %s %s %s %s"
  79. org-babel-haskell-compiler
  80. tmp-bin-file
  81. flags
  82. (org-babel-process-file-name tmp-src-file)
  83. libs)
  84. "")
  85. (let ((results (org-babel-eval (concat tmp-bin-file cmdline) "")))
  86. (when results
  87. (setq results (org-trim (org-remove-indentation results)))
  88. (org-babel-reassemble-table
  89. (org-babel-result-cond (cdr (assq :result-params params))
  90. (org-babel-read results t)
  91. (let ((tmp-file (org-babel-temp-file "Haskell-")))
  92. (with-temp-file tmp-file (insert results))
  93. (org-babel-import-elisp-from-file tmp-file)))
  94. (org-babel-pick-name
  95. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  96. (org-babel-pick-name
  97. (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))))
  98. (defun org-babel-interpret-haskell (body params)
  99. (require 'inf-haskell)
  100. (add-hook 'inferior-haskell-hook
  101. (lambda ()
  102. (setq-local comint-prompt-regexp
  103. (concat haskell-prompt-regexp "\\|^λ?> "))))
  104. (let* ((session (cdr (assq :session params)))
  105. (result-type (cdr (assq :result-type params)))
  106. (full-body (org-babel-expand-body:generic
  107. body params
  108. (org-babel-variable-assignments:haskell params)))
  109. (session (org-babel-haskell-initiate-session session params))
  110. (comint-preoutput-filter-functions
  111. (cons 'ansi-color-filter-apply comint-preoutput-filter-functions))
  112. (raw (org-babel-comint-with-output
  113. (session org-babel-haskell-eoe t full-body)
  114. (insert (org-trim full-body))
  115. (comint-send-input nil t)
  116. (insert org-babel-haskell-eoe)
  117. (comint-send-input nil t)))
  118. (results (mapcar #'org-strip-quotes
  119. (cdr (member org-babel-haskell-eoe
  120. (reverse (mapcar #'org-trim raw)))))))
  121. (org-babel-reassemble-table
  122. (let ((result
  123. (pcase result-type
  124. (`output (mapconcat #'identity (reverse results) "\n"))
  125. (`value (car results)))))
  126. (org-babel-result-cond (cdr (assq :result-params params))
  127. result (org-babel-script-escape result)))
  128. (org-babel-pick-name (cdr (assq :colname-names params))
  129. (cdr (assq :colname-names params)))
  130. (org-babel-pick-name (cdr (assq :rowname-names params))
  131. (cdr (assq :rowname-names params))))))
  132. (defun org-babel-execute:haskell (body params)
  133. "Execute a block of Haskell code."
  134. (let ((compile (string= "yes" (cdr (assq :compile params)))))
  135. (if (not compile)
  136. (org-babel-interpret-haskell body params)
  137. (org-babel-haskell-execute body params))))
  138. (defun org-babel-haskell-initiate-session (&optional _session _params)
  139. "Initiate a haskell session.
  140. If there is not a current inferior-process-buffer in SESSION
  141. then create one. Return the initialized session."
  142. (require 'inf-haskell)
  143. (or (get-buffer "*haskell*")
  144. (save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
  145. (defun org-babel-load-session:haskell (session body params)
  146. "Load BODY into SESSION."
  147. (save-window-excursion
  148. (let* ((buffer (org-babel-prep-session:haskell session params))
  149. (load-file (concat (org-babel-temp-file "haskell-load-") ".hs")))
  150. (with-temp-buffer
  151. (insert body) (write-file load-file)
  152. (haskell-mode) (inferior-haskell-load-file))
  153. buffer)))
  154. (defun org-babel-prep-session:haskell (session params)
  155. "Prepare SESSION according to the header arguments in PARAMS."
  156. (save-window-excursion
  157. (let ((buffer (org-babel-haskell-initiate-session session)))
  158. (org-babel-comint-in-buffer buffer
  159. (mapc (lambda (line)
  160. (insert line)
  161. (comint-send-input nil t))
  162. (org-babel-variable-assignments:haskell params)))
  163. (current-buffer))))
  164. (defun org-babel-variable-assignments:haskell (params)
  165. "Return list of haskell statements assigning the block's variables."
  166. (mapcar (lambda (pair)
  167. (format "let %s = %s"
  168. (car pair)
  169. (org-babel-haskell-var-to-haskell (cdr pair))))
  170. (org-babel--get-vars params)))
  171. (defun org-babel-haskell-var-to-haskell (var)
  172. "Convert an elisp value VAR into a haskell variable.
  173. The elisp VAR is converted to a string of haskell source code
  174. specifying a variable of the same value."
  175. (if (listp var)
  176. (concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var ", ") "]")
  177. (format "%S" var)))
  178. (defvar org-export-copy-to-kill-ring)
  179. (declare-function org-export-to-file "ox"
  180. (backend file
  181. &optional async subtreep visible-only body-only
  182. ext-plist post-process))
  183. (defun org-babel-haskell-export-to-lhs (&optional arg)
  184. "Export to a .lhs file with all haskell code blocks escaped.
  185. When called with a prefix argument the resulting
  186. .lhs file will be exported to a .tex file. This function will
  187. create two new files, base-name.lhs and base-name.tex where
  188. base-name is the name of the current Org file.
  189. Note that all standard Babel literate programming
  190. constructs (header arguments, no-web syntax etc...) are ignored."
  191. (interactive "P")
  192. (let* ((contents (buffer-string))
  193. (haskell-regexp
  194. (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)[\r\n]"
  195. "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
  196. (base-name (file-name-sans-extension (buffer-file-name)))
  197. (tmp-file (org-babel-temp-file "haskell-"))
  198. (tmp-org-file (concat tmp-file ".org"))
  199. (tmp-tex-file (concat tmp-file ".tex"))
  200. (lhs-file (concat base-name ".lhs"))
  201. (tex-file (concat base-name ".tex"))
  202. (command (concat org-babel-haskell-lhs2tex-command
  203. " " (org-babel-process-file-name lhs-file)
  204. " > " (org-babel-process-file-name tex-file)))
  205. (preserve-indentp org-src-preserve-indentation)
  206. indentation)
  207. ;; escape haskell source-code blocks
  208. (with-temp-file tmp-org-file
  209. (insert contents)
  210. (goto-char (point-min))
  211. (while (re-search-forward haskell-regexp nil t)
  212. (save-match-data (setq indentation (length (match-string 1))))
  213. (replace-match (save-match-data
  214. (concat
  215. "#+begin_export latex\n\\begin{code}\n"
  216. (if (or preserve-indentp
  217. (string-match "-i" (match-string 2)))
  218. (match-string 3)
  219. (org-remove-indentation (match-string 3)))
  220. "\n\\end{code}\n#+end_export\n"))
  221. t t)
  222. (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
  223. (save-excursion
  224. ;; export to latex w/org and save as .lhs
  225. (require 'ox-latex)
  226. (find-file tmp-org-file)
  227. ;; Ensure we do not clutter kill ring with incomplete results.
  228. (let (org-export-copy-to-kill-ring)
  229. (org-export-to-file 'latex tmp-tex-file))
  230. (kill-buffer nil)
  231. (delete-file tmp-org-file)
  232. (find-file tmp-tex-file)
  233. (goto-char (point-min)) (forward-line 2)
  234. (insert "%include polycode.fmt\n")
  235. ;; ensure all \begin/end{code} statements start at the first column
  236. (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t)
  237. (replace-match (save-match-data (org-remove-indentation (match-string 0)))
  238. t t))
  239. (setq contents (buffer-string))
  240. (save-buffer) (kill-buffer nil))
  241. (delete-file tmp-tex-file)
  242. ;; save org exported latex to a .lhs file
  243. (with-temp-file lhs-file (insert contents))
  244. (if (not arg)
  245. (find-file lhs-file)
  246. ;; process .lhs file with lhs2tex
  247. (message "running %s" command) (shell-command command) (find-file tex-file))))
  248. (provide 'ob-haskell)
  249. ;;; ob-haskell.el ends here