ob-haskell.el 8.4 KB

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