2010-06-11 23:02:42 +00:00
|
|
|
;;; ob-haskell.el --- org-babel functions for haskell evaluation
|
2009-08-27 14:03:02 +00:00
|
|
|
|
2013-01-08 14:04:32 +00:00
|
|
|
;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
|
2009-08-27 14:03:02 +00:00
|
|
|
|
|
|
|
;; Author: Eric Schulte
|
|
|
|
;; Keywords: literate programming, reproducible research
|
|
|
|
;; Homepage: http://orgmode.org
|
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-08-27 14:03:02 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-08-27 14:03:02 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 16:32:35 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2009-08-27 14:03:02 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
2010-06-25 16:32:35 +00:00
|
|
|
|
2009-08-27 14:03:02 +00:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2010-06-25 16:32:35 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2009-08-27 14:03:02 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Org-Babel support for evaluating haskell source code. This one will
|
|
|
|
;; be sort of tricky because haskell programs must be compiled before
|
|
|
|
;; they can be run, but haskell code can also be run through an
|
|
|
|
;; interactive interpreter.
|
|
|
|
;;
|
|
|
|
;; For now lets only allow evaluation using the haskell interpreter.
|
|
|
|
|
|
|
|
;;; Requirements:
|
|
|
|
|
|
|
|
;; - haskell-mode :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
|
|
|
|
;;
|
|
|
|
;; - inf-haskell :: http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
|
2009-10-04 22:36:49 +00:00
|
|
|
;;
|
|
|
|
;; - (optionally) lhs2tex :: http://people.cs.uu.nl/andres/lhs2tex/
|
2009-08-27 14:03:02 +00:00
|
|
|
|
|
|
|
;;; Code:
|
2010-06-11 23:02:42 +00:00
|
|
|
(require 'ob)
|
2010-07-03 02:21:31 +00:00
|
|
|
(require 'comint)
|
|
|
|
(eval-when-compile (require 'cl))
|
|
|
|
|
|
|
|
(declare-function org-remove-indentation "org" (code &optional n))
|
|
|
|
(declare-function haskell-mode "ext:haskell-mode" ())
|
|
|
|
(declare-function run-haskell "ext:inf-haskell" (&optional arg))
|
|
|
|
(declare-function inferior-haskell-load-file
|
|
|
|
"ext:inf-haskell" (&optional reload))
|
2009-08-27 14:03:02 +00:00
|
|
|
|
2011-06-21 22:34:41 +00:00
|
|
|
(defvar org-babel-tangle-lang-exts)
|
2010-06-17 17:21:14 +00:00
|
|
|
(add-to-list 'org-babel-tangle-lang-exts '("haskell" . "hs"))
|
|
|
|
|
2010-06-25 17:12:54 +00:00
|
|
|
(defvar org-babel-default-header-args:haskell '())
|
|
|
|
|
2009-10-04 23:23:50 +00:00
|
|
|
(defvar org-babel-haskell-lhs2tex-command "lhs2tex")
|
|
|
|
|
2009-08-27 14:03:02 +00:00
|
|
|
(defvar org-babel-haskell-eoe "\"org-babel-haskell-eoe\"")
|
|
|
|
|
|
|
|
(defun org-babel-execute:haskell (body params)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Execute a block of Haskell code."
|
2010-10-21 11:52:24 +00:00
|
|
|
(let* ((session (cdr (assoc :session params)))
|
|
|
|
(vars (mapcar #'cdr (org-babel-get-header params :var)))
|
|
|
|
(result-type (cdr (assoc :result-type params)))
|
2010-10-21 11:03:28 +00:00
|
|
|
(full-body (org-babel-expand-body:generic
|
|
|
|
body params
|
|
|
|
(org-babel-variable-assignments:haskell params)))
|
2010-06-28 16:55:09 +00:00
|
|
|
(session (org-babel-haskell-initiate-session session params))
|
2010-06-12 22:57:38 +00:00
|
|
|
(raw (org-babel-comint-with-output
|
2010-06-15 00:00:49 +00:00
|
|
|
(session org-babel-haskell-eoe t full-body)
|
2009-08-27 14:03:02 +00:00
|
|
|
(insert (org-babel-trim full-body))
|
|
|
|
(comint-send-input nil t)
|
|
|
|
(insert org-babel-haskell-eoe)
|
|
|
|
(comint-send-input nil t)))
|
|
|
|
(results (mapcar
|
|
|
|
#'org-babel-haskell-read-string
|
|
|
|
(cdr (member org-babel-haskell-eoe
|
|
|
|
(reverse (mapcar #'org-babel-trim raw)))))))
|
2010-04-19 01:06:34 +00:00
|
|
|
(org-babel-reassemble-table
|
2013-04-06 16:43:57 +00:00
|
|
|
((lambda (result)
|
|
|
|
(org-babel-result-cond (cdr (assoc :result-params params))
|
|
|
|
result (org-babel-haskell-table-or-string result)))
|
|
|
|
(case result-type
|
|
|
|
('output (mapconcat #'identity (reverse (cdr results)) "\n"))
|
|
|
|
('value (car results))))
|
2010-10-21 11:52:24 +00:00
|
|
|
(org-babel-pick-name (cdr (assoc :colname-names params))
|
|
|
|
(cdr (assoc :colname-names params)))
|
|
|
|
(org-babel-pick-name (cdr (assoc :rowname-names params))
|
|
|
|
(cdr (assoc :rowname-names params))))))
|
2009-08-27 14:03:02 +00:00
|
|
|
|
|
|
|
(defun org-babel-haskell-read-string (string)
|
2010-06-12 00:12:15 +00:00
|
|
|
"Strip \\\"s from around a haskell string."
|
2010-02-19 16:34:16 +00:00
|
|
|
(if (string-match "^\"\\([^\000]+\\)\"$" string)
|
2009-08-27 14:03:02 +00:00
|
|
|
(match-string 1 string)
|
|
|
|
string))
|
|
|
|
|
2010-03-13 17:59:41 +00:00
|
|
|
(defun org-babel-haskell-initiate-session (&optional session params)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Initiate a haskell session.
|
|
|
|
If there is not a current inferior-process-buffer in SESSION
|
2010-06-12 00:12:15 +00:00
|
|
|
then create one. Return the initialized session."
|
2010-07-03 02:21:31 +00:00
|
|
|
(require 'inf-haskell)
|
2010-06-28 16:55:09 +00:00
|
|
|
(or (get-buffer "*haskell*")
|
|
|
|
(save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
|
2009-09-25 01:16:42 +00:00
|
|
|
|
2010-10-16 03:35:45 +00:00
|
|
|
(defun org-babel-load-session:haskell (session body params)
|
2009-09-25 03:58:28 +00:00
|
|
|
"Load BODY into SESSION."
|
|
|
|
(save-window-excursion
|
2010-10-16 03:35:45 +00:00
|
|
|
(let* ((buffer (org-babel-prep-session:haskell session params))
|
2010-08-25 20:43:07 +00:00
|
|
|
(load-file (concat (org-babel-temp-file "haskell-load-") ".hs")))
|
2009-09-25 03:58:28 +00:00
|
|
|
(with-temp-buffer
|
|
|
|
(insert body) (write-file load-file)
|
|
|
|
(haskell-mode) (inferior-haskell-load-file))
|
|
|
|
buffer)))
|
|
|
|
|
2010-10-16 03:35:45 +00:00
|
|
|
(defun org-babel-prep-session:haskell (session params)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Prepare SESSION according to the header arguments in PARAMS."
|
2009-09-25 01:16:42 +00:00
|
|
|
(save-window-excursion
|
2010-10-21 11:03:28 +00:00
|
|
|
(let ((buffer (org-babel-haskell-initiate-session session)))
|
2010-06-28 16:55:09 +00:00
|
|
|
(org-babel-comint-in-buffer buffer
|
2010-10-21 11:03:28 +00:00
|
|
|
(mapc (lambda (line)
|
|
|
|
(insert line)
|
|
|
|
(comint-send-input nil t))
|
|
|
|
(org-babel-variable-assignments:haskell params)))
|
2009-09-25 01:16:42 +00:00
|
|
|
(current-buffer))))
|
2009-08-27 14:03:02 +00:00
|
|
|
|
2010-10-21 11:03:28 +00:00
|
|
|
(defun org-babel-variable-assignments:haskell (params)
|
2012-07-30 08:08:15 +00:00
|
|
|
"Return list of haskell statements assigning the block's variables."
|
2010-10-21 11:03:28 +00:00
|
|
|
(mapcar (lambda (pair)
|
|
|
|
(format "let %s = %s"
|
|
|
|
(car pair)
|
|
|
|
(org-babel-haskell-var-to-haskell (cdr pair))))
|
2012-08-11 17:10:44 +00:00
|
|
|
(mapcar #'cdr (org-babel-get-header params :var))))
|
2010-10-21 11:03:28 +00:00
|
|
|
|
2009-08-27 14:03:02 +00:00
|
|
|
(defun org-babel-haskell-table-or-string (results)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Convert RESULTS to an Emacs-lisp table or string.
|
|
|
|
If RESULTS look like a table, then convert them into an
|
2009-08-27 14:03:02 +00:00
|
|
|
Emacs-lisp table, otherwise return the results as a string."
|
2010-11-23 01:20:56 +00:00
|
|
|
(org-babel-script-escape results))
|
2009-08-27 14:03:02 +00:00
|
|
|
|
2010-06-28 16:55:09 +00:00
|
|
|
(defun org-babel-haskell-var-to-haskell (var)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Convert an elisp value VAR into a haskell variable.
|
|
|
|
The elisp VAR is converted to a string of haskell source code
|
|
|
|
specifying a variable of the same value."
|
2010-06-28 16:55:09 +00:00
|
|
|
(if (listp var)
|
|
|
|
(concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var ", ") "]")
|
|
|
|
(format "%S" var)))
|
|
|
|
|
2010-07-03 02:21:31 +00:00
|
|
|
(defvar org-src-preserve-indentation)
|
2013-01-27 22:44:27 +00:00
|
|
|
(declare-function org-export-to-file "ox"
|
|
|
|
(backend file
|
Export back-ends: Apply changes to export functions
* contrib/lisp/ox-confluence.el (org-confluence-export-as-confluence):
* contrib/lisp/ox-deck.el (org-deck-export-as-html,
org-deck-export-to-html):
* contrib/lisp/ox-freemind.el (org-freemind-export-to-freemind):
* contrib/lisp/ox-groff.el (org-groff-export-to-groff,
org-groff-export-to-pdf):
* contrib/lisp/ox-koma-letter.el (org-koma-letter-export-as-latex,
org-koma-letter-export-to-latex, org-koma-letter-export-to-pdf):
* contrib/lisp/ox-rss.el (org-rss-export-as-rss,
org-rss-export-to-rss):
* contrib/lisp/ox-s5.el (org-s5-export-as-html,
org-s5-export-to-html):
* contrib/lisp/ox-taskjuggler.el (org-taskjuggler-export):
* lisp/ob-haskell.el:
* lisp/ox-ascii.el (org-ascii-export-as-ascii,
org-ascii-export-to-ascii):
* lisp/ox-beamer.el (org-beamer-export-as-latex,
org-beamer-export-to-latex, org-beamer-export-to-pdf):
* lisp/ox-html.el (org-html-export-as-html, org-html-export-to-html):
* lisp/ox-icalendar.el (org-icalendar-export-to-ics):
* lisp/ox-latex.el (org-latex-export-as-latex,
org-latex-export-to-pdf):
* lisp/ox-man.el (org-man-export-to-man, org-man-export-to-pdf):
* lisp/ox-md.el (org-md-export-as-markdown,
org-md-export-to-markdown):
* lisp/ox-odt.el (org-odt-export-to-odt):
* lisp/ox-org.el (org-org-export-as-org, org-org-export-to-org):
* lisp/ox-publish.el (org-publish-org-to):
* lisp/ox-texinfo.el (org-texinfo-export-to-texinfo,
org-texinfo-export-to-info):
* testing/lisp/test-ob-exp.el (test-ob-exp/org-babel-exp-src-blocks/w-no-file):
2013-08-07 08:35:42 +00:00
|
|
|
&optional async subtreep visible-only body-only ext-plist))
|
2009-10-09 17:42:09 +00:00
|
|
|
(defun org-babel-haskell-export-to-lhs (&optional arg)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Export to a .lhs file with all haskell code blocks escaped.
|
|
|
|
When called with a prefix argument the resulting
|
2009-10-09 17:42:09 +00:00
|
|
|
.lhs file will be exported to a .tex file. 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.
|
2009-10-04 22:36:49 +00:00
|
|
|
|
2010-07-13 23:20:08 +00:00
|
|
|
Note that all standard Babel literate programming
|
2009-10-04 22:36:49 +00:00
|
|
|
constructs (header arguments, no-web syntax etc...) are ignored."
|
2009-10-09 17:42:09 +00:00
|
|
|
(interactive "P")
|
2009-10-04 22:36:49 +00:00
|
|
|
(let* ((contents (buffer-string))
|
|
|
|
(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)))
|
2010-08-25 20:43:07 +00:00
|
|
|
(tmp-file (org-babel-temp-file "haskell-"))
|
2009-10-04 22:36:49 +00:00
|
|
|
(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"))
|
2010-09-22 20:40:14 +00:00
|
|
|
(command (concat org-babel-haskell-lhs2tex-command
|
|
|
|
" " (org-babel-process-file-name lhs-file)
|
|
|
|
" > " (org-babel-process-file-name tex-file)))
|
2009-11-05 00:46:55 +00:00
|
|
|
(preserve-indentp org-src-preserve-indentation)
|
2009-10-04 22:36:49 +00:00
|
|
|
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))))
|
2009-11-05 00:46:55 +00:00
|
|
|
(replace-match (save-match-data
|
|
|
|
(concat
|
|
|
|
"#+begin_latex\n\\begin{code}\n"
|
|
|
|
(if (or preserve-indentp
|
|
|
|
(string-match "-i" (match-string 2)))
|
|
|
|
(match-string 3)
|
|
|
|
(org-remove-indentation (match-string 3)))
|
|
|
|
"\n\\end{code}\n#+end_latex\n"))
|
2009-10-04 22:36:49 +00:00
|
|
|
t t)
|
|
|
|
(indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
|
|
|
|
(save-excursion
|
|
|
|
;; export to latex w/org and save as .lhs
|
2013-01-27 22:44:27 +00:00
|
|
|
(require 'ox-latex)
|
|
|
|
(find-file tmp-org-file)
|
|
|
|
;; Ensure we do not clutter kill ring with incomplete results.
|
|
|
|
(let (org-export-copy-to-kill-ring)
|
|
|
|
(org-export-to-file 'latex tmp-tex-file))
|
2011-07-02 14:15:14 +00:00
|
|
|
(kill-buffer nil)
|
2009-10-04 22:36:49 +00:00
|
|
|
(delete-file tmp-org-file)
|
|
|
|
(find-file tmp-tex-file)
|
|
|
|
(goto-char (point-min)) (forward-line 2)
|
|
|
|
(insert "%include polycode.fmt\n")
|
2009-10-07 22:01:24 +00:00
|
|
|
;; ensure all \begin/end{code} statements start at the first column
|
|
|
|
(while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t)
|
|
|
|
(replace-match (save-match-data (org-remove-indentation (match-string 0)))
|
|
|
|
t t))
|
2009-10-04 22:36:49 +00:00
|
|
|
(setq contents (buffer-string))
|
2011-07-02 14:15:14 +00:00
|
|
|
(save-buffer) (kill-buffer nil))
|
2009-10-04 22:36:49 +00:00
|
|
|
(delete-file tmp-tex-file)
|
|
|
|
;; save org exported latex to a .lhs file
|
|
|
|
(with-temp-file lhs-file (insert contents))
|
2009-10-09 17:42:09 +00:00
|
|
|
(if (not arg)
|
|
|
|
(find-file lhs-file)
|
|
|
|
;; process .lhs file with lhs2tex
|
|
|
|
(message "running %s" command) (shell-command command) (find-file tex-file))))
|
2009-10-04 22:36:49 +00:00
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
(provide 'ob-haskell)
|
2010-06-25 16:32:35 +00:00
|
|
|
|
2011-08-15 18:04:38 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
;;; ob-haskell.el ends here
|