ob-haskell.el 8.8 KB

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