ob-haskell.el 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 'haskell-mode)
  34. (require 'inf-haskell)
  35. (add-to-list 'org-babel-tangle-lang-exts '("haskell" . "hs"))
  36. (defvar org-babel-default-header-args:haskell '())
  37. (defvar org-babel-haskell-lhs2tex-command "lhs2tex")
  38. (defvar org-babel-haskell-eoe "\"org-babel-haskell-eoe\"")
  39. (defun org-babel-expand-body:haskell (body params &optional processed-params)
  40. "Expand BODY according to PARAMS, return the expanded body."
  41. (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
  42. (concat
  43. (mapconcat
  44. (lambda (pair) (format "let %s = %s"
  45. (car pair)
  46. (org-babel-haskell-var-to-haskell (cdr pair))))
  47. vars "\n") "\n" body "\n")))
  48. (defun org-babel-execute:haskell (body params)
  49. "Execute a block of Haskell code with org-babel."
  50. (message "executing haskell source code block")
  51. (let* ((processed-params (org-babel-process-params params))
  52. (session (first processed-params))
  53. (vars (nth 1 processed-params))
  54. (result-type (nth 3 processed-params))
  55. (full-body (org-babel-expand-body:haskell body params processed-params))
  56. (session (org-babel-haskell-initiate-session session params))
  57. (raw (org-babel-comint-with-output
  58. (session org-babel-haskell-eoe t full-body)
  59. (insert (org-babel-trim full-body))
  60. (comint-send-input nil t)
  61. (insert org-babel-haskell-eoe)
  62. (comint-send-input nil t)))
  63. (results (mapcar
  64. #'org-babel-haskell-read-string
  65. (cdr (member org-babel-haskell-eoe
  66. (reverse (mapcar #'org-babel-trim raw)))))))
  67. (org-babel-reassemble-table
  68. (case result-type
  69. (output (mapconcat #'identity (reverse (cdr results)) "\n"))
  70. (value (org-babel-haskell-table-or-string (car results))))
  71. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  72. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params))))))
  73. (defun org-babel-haskell-read-string (string)
  74. "Strip \\\"s from around a haskell string."
  75. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  76. (match-string 1 string)
  77. string))
  78. (defun org-babel-haskell-initiate-session (&optional session params)
  79. "If there is not a current inferior-process-buffer in SESSION
  80. then create one. Return the initialized session."
  81. ;; TODO: make it possible to have multiple sessions
  82. (or (get-buffer "*haskell*")
  83. (save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
  84. (defun org-babel-load-session:haskell
  85. (session body params &optional processed-params)
  86. "Load BODY into SESSION."
  87. (save-window-excursion
  88. (let* ((buffer (org-babel-prep-session:haskell
  89. session params processed-params))
  90. (load-file (concat (make-temp-file "org-babel-haskell-load") ".hs")))
  91. (with-temp-buffer
  92. (insert body) (write-file load-file)
  93. (haskell-mode) (inferior-haskell-load-file))
  94. buffer)))
  95. (defun org-babel-prep-session:haskell
  96. (session params &optional processesed-params)
  97. "Prepare SESSION according to the header arguments specified in PARAMS."
  98. (save-window-excursion
  99. (let ((pp (or processed-params (org-babel-process-params params)))
  100. (buffer (org-babel-haskell-initiate-session session)))
  101. (org-babel-comint-in-buffer buffer
  102. (mapcar
  103. (lambda (pair)
  104. (insert (format "let %s = %s"
  105. (car pair)
  106. (org-babel-haskell-var-to-haskell (cdr pair))))
  107. (comint-send-input nil t))
  108. (nth 1 pp)))
  109. (current-buffer))))
  110. (defun org-babel-haskell-table-or-string (results)
  111. "If the results look like a table, then convert them into an
  112. Emacs-lisp table, otherwise return the results as a string."
  113. (org-babel-read
  114. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  115. (org-babel-read
  116. (concat "'"
  117. (replace-regexp-in-string
  118. "\\[" "(" (replace-regexp-in-string
  119. "\\]" ")" (replace-regexp-in-string
  120. "," " " (replace-regexp-in-string
  121. "'" "\"" results))))))
  122. results)))
  123. (defun org-babel-haskell-var-to-haskell (var)
  124. "Convert an elisp var into a string of haskell source code
  125. specifying a var of the same value."
  126. (if (listp var)
  127. (concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var ", ") "]")
  128. (format "%S" var)))
  129. (defun org-babel-haskell-export-to-lhs (&optional arg)
  130. "Export to a .lhs file with all haskell code blocks escaped
  131. appropriately. When called with a prefix argument the resulting
  132. .lhs file will be exported to a .tex file. This function will
  133. create two new files, base-name.lhs and base-name.tex where
  134. base-name is the name of the current org-mode file.
  135. Note that all standard org-babel literate programming
  136. constructs (header arguments, no-web syntax etc...) are ignored."
  137. (interactive "P")
  138. (let* ((contents (buffer-string))
  139. (haskell-regexp
  140. (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
  141. "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
  142. (base-name (file-name-sans-extension (buffer-file-name)))
  143. (tmp-file (make-temp-file "ob-haskell"))
  144. (tmp-org-file (concat tmp-file ".org"))
  145. (tmp-tex-file (concat tmp-file ".tex"))
  146. (lhs-file (concat base-name ".lhs"))
  147. (tex-file (concat base-name ".tex"))
  148. (command (concat org-babel-haskell-lhs2tex-command " " lhs-file " > " 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_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_latex\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. (find-file tmp-org-file) (funcall 'org-export-as-latex nil)
  170. (kill-buffer)
  171. (delete-file tmp-org-file)
  172. (find-file tmp-tex-file)
  173. (goto-char (point-min)) (forward-line 2)
  174. (insert "%include polycode.fmt\n")
  175. ;; ensure all \begin/end{code} statements start at the first column
  176. (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t)
  177. (replace-match (save-match-data (org-remove-indentation (match-string 0)))
  178. t t))
  179. (setq contents (buffer-string))
  180. (save-buffer) (kill-buffer))
  181. (delete-file tmp-tex-file)
  182. ;; save org exported latex to a .lhs file
  183. (with-temp-file lhs-file (insert contents))
  184. (if (not arg)
  185. (find-file lhs-file)
  186. ;; process .lhs file with lhs2tex
  187. (message "running %s" command) (shell-command command) (find-file tex-file))))
  188. (provide 'ob-haskell)
  189. ;; arch-tag: b53f75f3-ba1a-4b05-82d9-a2a0d4e70804
  190. ;;; ob-haskell.el ends here