ob-haskell.el 11 KB

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