ob-haskell.el 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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: 7.01trans
  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."
  56. (let* ((processed-params (org-babel-process-params params))
  57. (session (nth 0 processed-params))
  58. (vars (nth 1 processed-params))
  59. (result-type (nth 3 processed-params))
  60. (full-body (org-babel-expand-body:haskell body params processed-params))
  61. (session (org-babel-haskell-initiate-session session params))
  62. (raw (org-babel-comint-with-output
  63. (session org-babel-haskell-eoe t full-body)
  64. (insert (org-babel-trim full-body))
  65. (comint-send-input nil t)
  66. (insert org-babel-haskell-eoe)
  67. (comint-send-input nil t)))
  68. (results (mapcar
  69. #'org-babel-haskell-read-string
  70. (cdr (member org-babel-haskell-eoe
  71. (reverse (mapcar #'org-babel-trim raw)))))))
  72. (org-babel-reassemble-table
  73. (cond
  74. ((equal result-type 'output)
  75. (mapconcat #'identity (reverse (cdr results)) "\n"))
  76. ((equal result-type 'value)
  77. (org-babel-haskell-table-or-string (car results))))
  78. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  79. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params))))))
  80. (defun org-babel-haskell-read-string (string)
  81. "Strip \\\"s from around a haskell string."
  82. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  83. (match-string 1 string)
  84. string))
  85. (defun org-babel-haskell-initiate-session (&optional session params)
  86. "Initiate a haskell session.
  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 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. "Convert RESULTS to an Emacs-lisp table or string.
  120. If RESULTS look like a table, then convert them into an
  121. Emacs-lisp table, otherwise return the results as a string."
  122. (org-babel-read
  123. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  124. (org-babel-read
  125. (concat "'"
  126. (replace-regexp-in-string
  127. "\\[" "(" (replace-regexp-in-string
  128. "\\]" ")" (replace-regexp-in-string
  129. "," " " (replace-regexp-in-string
  130. "'" "\"" results))))))
  131. results)))
  132. (defun org-babel-haskell-var-to-haskell (var)
  133. "Convert an elisp value VAR into a haskell variable.
  134. The elisp VAR is converted to a string of haskell source code
  135. specifying a variable of the same value."
  136. (if (listp var)
  137. (concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var ", ") "]")
  138. (format "%S" var)))
  139. (defvar org-src-preserve-indentation)
  140. (defun org-babel-haskell-export-to-lhs (&optional arg)
  141. "Export to a .lhs file with all haskell code blocks escaped.
  142. When called with a prefix argument the resulting
  143. .lhs file will be exported to a .tex file. This function will
  144. create two new files, base-name.lhs and base-name.tex where
  145. base-name is the name of the current org-mode file.
  146. Note that all standard Babel literate programming
  147. constructs (header arguments, no-web syntax etc...) are ignored."
  148. (interactive "P")
  149. (let* ((contents (buffer-string))
  150. (haskell-regexp
  151. (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
  152. "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
  153. (base-name (file-name-sans-extension (buffer-file-name)))
  154. (tmp-file (make-temp-file "ob-haskell"))
  155. (tmp-org-file (concat tmp-file ".org"))
  156. (tmp-tex-file (concat tmp-file ".tex"))
  157. (lhs-file (concat base-name ".lhs"))
  158. (tex-file (concat base-name ".tex"))
  159. (command (concat org-babel-haskell-lhs2tex-command " " lhs-file " > " tex-file))
  160. (preserve-indentp org-src-preserve-indentation)
  161. indentation)
  162. ;; escape haskell source-code blocks
  163. (with-temp-file tmp-org-file
  164. (insert contents)
  165. (goto-char (point-min))
  166. (while (re-search-forward haskell-regexp nil t)
  167. (save-match-data (setq indentation (length (match-string 1))))
  168. (replace-match (save-match-data
  169. (concat
  170. "#+begin_latex\n\\begin{code}\n"
  171. (if (or preserve-indentp
  172. (string-match "-i" (match-string 2)))
  173. (match-string 3)
  174. (org-remove-indentation (match-string 3)))
  175. "\n\\end{code}\n#+end_latex\n"))
  176. t t)
  177. (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
  178. (save-excursion
  179. ;; export to latex w/org and save as .lhs
  180. (find-file tmp-org-file) (funcall 'org-export-as-latex nil)
  181. (kill-buffer)
  182. (delete-file tmp-org-file)
  183. (find-file tmp-tex-file)
  184. (goto-char (point-min)) (forward-line 2)
  185. (insert "%include polycode.fmt\n")
  186. ;; ensure all \begin/end{code} statements start at the first column
  187. (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t)
  188. (replace-match (save-match-data (org-remove-indentation (match-string 0)))
  189. t t))
  190. (setq contents (buffer-string))
  191. (save-buffer) (kill-buffer))
  192. (delete-file tmp-tex-file)
  193. ;; save org exported latex to a .lhs file
  194. (with-temp-file lhs-file (insert contents))
  195. (if (not arg)
  196. (find-file lhs-file)
  197. ;; process .lhs file with lhs2tex
  198. (message "running %s" command) (shell-command command) (find-file tex-file))))
  199. (provide 'ob-haskell)
  200. ;; arch-tag: b53f75f3-ba1a-4b05-82d9-a2a0d4e70804
  201. ;;; ob-haskell.el ends here