summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Schulte <schulte.eric@gmail.com>2009-10-04 16:36:49 -0600
committerEric Schulte <schulte.eric@gmail.com>2009-10-04 16:41:45 -0600
commit63b360041fdefc4d75480fb373d828484dde8170 (patch)
tree22bbe69b6d61118bcf7439c26a7da3d3889c0c9a
parentce4aad900a532c2aa076f9a8ff38c875403a3c93 (diff)
downloadorg-mode-63b360041fdefc4d75480fb373d828484dde8170.tar.gz
org-babel: org-babel-haskell can now export to .lhs literate Haskell
this commit adds the function `org-babel-haskell-export-to-lhs' for information on lhs see http://people.cs.uu.nl/andres/lhs2tex/
-rw-r--r--contrib/babel/lisp/langs/org-babel-haskell.el56
1 files changed, 56 insertions, 0 deletions
diff --git a/contrib/babel/lisp/langs/org-babel-haskell.el b/contrib/babel/lisp/langs/org-babel-haskell.el
index ab6cb71..f7494ea 100644
--- a/contrib/babel/lisp/langs/org-babel-haskell.el
+++ b/contrib/babel/lisp/langs/org-babel-haskell.el
@@ -38,6 +38,8 @@
;; - haskell-mode :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
;;
;; - inf-haskell :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
+;;
+;; - (optionally) lhs2tex :: http://people.cs.uu.nl/andres/lhs2tex/
;;; Code:
(require 'org-babel)
@@ -128,5 +130,59 @@ Emacs-lisp table, otherwise return the results as a string."
"'" "\"" results)))))
results)))
+(defun org-babel-haskell-export-to-lhs ()
+ "Export to a .lhs with all haskell code blocks escaped
+appropriately, then process the resulting .lhs file using
+lhs2tex. This function will create two new files, base-name.lhs
+and base-name.tex where base-name is the name of the current
+org-mode file.
+
+Note that all standard org-babel literate programming
+constructs (header arguments, no-web syntax etc...) are ignored."
+ (interactive)
+ (let* ((contents (buffer-string))
+ (lhs2tex-command "~/.cabal/bin/lhs2tex")
+ (haskell-regexp
+ (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)?[\r\n]"
+ "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
+ (base-name (file-name-sans-extension (buffer-file-name)))
+ (tmp-file (make-temp-file "ob-haskell"))
+ (tmp-org-file (concat tmp-file ".org"))
+ (tmp-tex-file (concat tmp-file ".tex"))
+ (lhs-file (concat base-name ".lhs"))
+ (tex-file (concat base-name ".tex"))
+ indentation)
+ ;; escape haskell source-code blocks
+ (with-temp-file tmp-org-file
+ (insert contents)
+ (goto-char (point-min))
+ (while (re-search-forward haskell-regexp nil t)
+ (save-match-data (setq indentation (length (match-string 1))))
+ (replace-match (save-match-data (concat
+ "#+begin_latex\n\\begin{code}\n"
+ (org-remove-indentation (match-string 3))
+ "\n\\end{code}\n#+end_latex\n"))
+ t t)
+ (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
+ (save-excursion
+ ;; export to latex w/org and save as .lhs
+ (find-file tmp-org-file) (call-interactively 'org-export-as-latex)
+ (kill-buffer)
+ (delete-file tmp-org-file)
+ (find-file tmp-tex-file)
+ (goto-char (point-min)) (forward-line 2)
+ (insert "%include polycode.fmt\n")
+ (setq contents (buffer-string))
+ (save-buffer) (kill-buffer))
+ (delete-file tmp-tex-file)
+ ;; save org exported latex to a .lhs file
+ (with-temp-file lhs-file (insert contents))
+ ;; process .lhs file with lhs2tex and place results in .tex file
+ (with-temp-file tex-file
+ (insert (shell-command-to-string (concat lhs2tex-command " " lhs-file))))
+ (message "created %s and %s"
+ (file-name-nondirectory lhs-file)
+ (file-name-nondirectory tex-file))))
+
(provide 'org-babel-haskell)
;;; org-babel-haskell.el ends here