ob-haskell.el 8.7 KB

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