ob-haskell.el 9.2 KB

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