2015-10-26 00:56:00 +00:00
|
|
|
;;; ob-haskell.el --- Babel Functions for Haskell -*- lexical-binding: t; -*-
|
2009-08-27 14:03:02 +00:00
|
|
|
|
2021-01-01 19:55:31 +00:00
|
|
|
;; Copyright (C) 2009-2021 Free Software Foundation, Inc.
|
2009-08-27 14:03:02 +00:00
|
|
|
|
|
|
|
;; Author: Eric Schulte
|
2020-12-17 05:47:14 +00:00
|
|
|
;; Maintainer: Lawrence Bottorff <borgauf@gmail.com>
|
2009-08-27 14:03:02 +00:00
|
|
|
;; Keywords: literate programming, reproducible research
|
2018-01-16 16:22:00 +00:00
|
|
|
;; Homepage: https://orgmode.org
|
2009-08-27 14:03:02 +00:00
|
|
|
|
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
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2009-08-27 14:03:02 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2020-05-23 14:46:26 +00:00
|
|
|
;; Org Babel support for evaluating Haskell source code.
|
|
|
|
;; Haskell programs must be compiled before
|
2009-08-27 14:03:02 +00:00
|
|
|
;; they can be run, but haskell code can also be run through an
|
|
|
|
;; interactive interpreter.
|
|
|
|
;;
|
2020-05-23 14:46:26 +00:00
|
|
|
;; By default we evaluate using the Haskell interpreter.
|
|
|
|
;; To use the compiler, specify :compile yes in the header.
|
2009-08-27 14:03:02 +00:00
|
|
|
|
|
|
|
;;; Requirements:
|
|
|
|
|
2021-03-21 18:55:14 +00:00
|
|
|
;; - haskell-mode: https://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
|
|
|
|
;; - inf-haskell: https://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
|
|
|
|
;; - (optionally) lhs2tex: https://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)
|
2018-05-10 00:04:12 +00:00
|
|
|
(require 'org-macs)
|
2010-07-03 02:21:31 +00:00
|
|
|
(require 'comint)
|
|
|
|
|
|
|
|
(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))
|
2020-05-23 14:46:26 +00:00
|
|
|
(declare-function org-entry-get "org" (pom property &optional inherit literal-nil))
|
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"))
|
|
|
|
|
2013-09-18 21:19:54 +00:00
|
|
|
(defvar org-babel-default-header-args:haskell
|
|
|
|
'((:padlines . "no")))
|
2010-06-25 17:12:54 +00:00
|
|
|
|
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\"")
|
|
|
|
|
2017-12-02 21:25:54 +00:00
|
|
|
(defvar haskell-prompt-regexp)
|
|
|
|
|
2020-05-23 14:46:26 +00:00
|
|
|
(defcustom org-babel-haskell-compiler "ghc"
|
|
|
|
"Command used to compile a Haskell source code file into an executable.
|
|
|
|
May be either a command in the path, like \"ghc\" or an absolute
|
|
|
|
path name, like \"/usr/local/bin/ghc\". The command can include
|
|
|
|
a parameter, such as \"ghc -v\"."
|
|
|
|
:group 'org-babel
|
|
|
|
:package-version '(Org "9.4")
|
|
|
|
:type 'string)
|
|
|
|
|
|
|
|
(defconst org-babel-header-args:haskell '(compile . :any)
|
|
|
|
"Haskell-specific header arguments.")
|
|
|
|
|
|
|
|
(defun org-babel-haskell-execute (body params)
|
|
|
|
"This function should only be called by `org-babel-execute:haskell'"
|
|
|
|
(let* ((tmp-src-file (org-babel-temp-file "Haskell-src-" ".hs"))
|
|
|
|
(tmp-bin-file
|
|
|
|
(org-babel-process-file-name
|
|
|
|
(org-babel-temp-file "Haskell-bin-" org-babel-exeext)))
|
|
|
|
(cmdline (cdr (assq :cmdline params)))
|
|
|
|
(cmdline (if cmdline (concat " " cmdline) ""))
|
|
|
|
(flags (cdr (assq :flags params)))
|
|
|
|
(flags (mapconcat #'identity
|
|
|
|
(if (listp flags)
|
|
|
|
flags
|
|
|
|
(list flags))
|
|
|
|
" "))
|
|
|
|
(libs (org-babel-read
|
|
|
|
(or (cdr (assq :libs params))
|
|
|
|
(org-entry-get nil "libs" t))
|
|
|
|
nil))
|
|
|
|
(libs (mapconcat #'identity
|
|
|
|
(if (listp libs) libs (list libs))
|
|
|
|
" ")))
|
|
|
|
(with-temp-file tmp-src-file (insert body))
|
|
|
|
(org-babel-eval
|
|
|
|
(format "%s -o %s %s %s %s"
|
|
|
|
org-babel-haskell-compiler
|
|
|
|
tmp-bin-file
|
|
|
|
flags
|
|
|
|
(org-babel-process-file-name tmp-src-file)
|
|
|
|
libs)
|
|
|
|
"")
|
|
|
|
(let ((results (org-babel-eval (concat tmp-bin-file cmdline) "")))
|
|
|
|
(when results
|
|
|
|
(setq results (org-trim (org-remove-indentation results)))
|
|
|
|
(org-babel-reassemble-table
|
|
|
|
(org-babel-result-cond (cdr (assq :result-params params))
|
|
|
|
(org-babel-read results t)
|
|
|
|
(let ((tmp-file (org-babel-temp-file "Haskell-")))
|
|
|
|
(with-temp-file tmp-file (insert results))
|
|
|
|
(org-babel-import-elisp-from-file tmp-file)))
|
|
|
|
(org-babel-pick-name
|
|
|
|
(cdr (assq :colname-names params)) (cdr (assq :colnames params)))
|
|
|
|
(org-babel-pick-name
|
|
|
|
(cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))))
|
|
|
|
|
|
|
|
(defun org-babel-interpret-haskell (body params)
|
2017-12-02 21:25:54 +00:00
|
|
|
(require 'inf-haskell)
|
|
|
|
(add-hook 'inferior-haskell-hook
|
|
|
|
(lambda ()
|
|
|
|
(setq-local comint-prompt-regexp
|
|
|
|
(concat haskell-prompt-regexp "\\|^λ?> "))))
|
2016-09-22 17:45:15 +00:00
|
|
|
(let* ((session (cdr (assq :session params)))
|
|
|
|
(result-type (cdr (assq :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))
|
2017-12-25 17:29:27 +00:00
|
|
|
(comint-preoutput-filter-functions
|
2018-10-06 07:40:33 +00:00
|
|
|
(cons 'ansi-color-filter-apply comint-preoutput-filter-functions))
|
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)
|
2016-06-21 13:24:06 +00:00
|
|
|
(insert (org-trim full-body))
|
2009-08-27 14:03:02 +00:00
|
|
|
(comint-send-input nil t)
|
|
|
|
(insert org-babel-haskell-eoe)
|
|
|
|
(comint-send-input nil t)))
|
2018-10-06 07:40:33 +00:00
|
|
|
(results (mapcar #'org-strip-quotes
|
|
|
|
(cdr (member org-babel-haskell-eoe
|
|
|
|
(reverse (mapcar #'org-trim raw)))))))
|
2010-04-19 01:06:34 +00:00
|
|
|
(org-babel-reassemble-table
|
Backport changes from Emacs revs 115081 and 115082
2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca>
Address some byte-compiler warnings.
* ob-abc.el (org-babel-expand-body:abc): Use dolist.
(org-babel-execute:abc): Fix regexp quoting.
* ob-calc.el (org--var-syms): Rename from `var-syms'.
* ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding.
* ob-table.el (sbe): Move debug declaration.
* org-clock.el (org--msg-extra): Rename from `msg-extra'.
* org.el (org-version): Avoid var name starting with _.
(org-inhibit-startup, org-called-with-limited-levels)
(org-link-search-inhibit-query, org-time-was-given)
(org-end-time-was-given, org-def, org-defdecode, org-with-time):
* org-colview.el (org-agenda-overriding-columns-format):
* org-agenda.el (org-agenda-multi, org-depend-tag-blocked)
(org-agenda-show-log-scoped):
* ob-python.el (py-which-bufname, python-shell-buffer-name):
* ob-haskell.el (org-export-copy-to-kill-ring):
* ob-exp.el (org-link-search-inhibit-query):
* ob-R.el (ess-eval-visibly-p):
* ob-core.el (org-src-window-setup): Declare before use.
(org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'.
* ox-odt.el (org-odt-hfy-face-to-css):
* org-src.el (org-src-associate-babel-session, org-src-get-lang-mode):
* org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex)
(org-bibtex-check):
* ob-tangle.el (org-babel-tangle, org-babel-spec-to-string)
(org-babel-tangle-single-block, org-babel-tangle-comment-links):
* ob-table.el (sbe):
* ob-sqlite.el (org-babel-sqlite-expand-vars):
* ob-sql.el (org-babel-sql-expand-vars):
* ob-shen.el (org-babel-execute:shen):
* ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate):
* ob-scala.el (org-babel-scala-evaluate):
* ob-ruby.el (org-babel-ruby-table-or-string)
(org-babel-ruby-evaluate):
* ob-python.el (org-babel-python-table-or-string)
(org-babel-python-evaluate-external-process)
(org-babel-python-evaluate-session):
* ob-picolisp.el (org-babel-execute:picolisp):
* ob-perl.el (org-babel-perl-evaluate):
* ob-maxima.el (org-babel-execute:maxima):
* ob-lisp.el (org-babel-execute:lisp):
* ob-java.el (org-babel-execute:java):
* ob-io.el (org-babel-io-evaluate):
* ob-haskell.el (org-babel-execute:haskell):
* ob-fortran.el (org-babel-execute:fortran):
* ob-exp.el (org-babel-exp-code):
* ob-emacs-lisp.el (org-babel-execute:emacs-lisp):
* ob-ditaa.el (org-babel-execute:ditaa):
* ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash)
(org-babel-parse-header-arguments, org-babel-reassemble-table)
(org-babel-goto-src-block-head, org-babel-mark-block)
(org-babel-expand-noweb-references, org-babel-script-escape)
(org-babel-process-file-name):
* ob-clojure.el (org-babel-execute:clojure):
* ob-calc.el (org-babel-execute:calc):
* ob-awk.el (org-babel-execute:awk):
* ob-abc.el (org-babel-execute:abc):
* ob-R.el (org-babel-expand-body:R):
* ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...).
2013-11-12 Glenn Morris <rgm@gnu.org>
* ox-html.el (org-html-scripts): Add 2013 to copyright years.
(org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
|
|
|
(let ((result
|
2016-07-25 14:34:48 +00:00
|
|
|
(pcase result-type
|
2020-05-04 14:56:38 +00:00
|
|
|
(`output (mapconcat #'identity (reverse results) "\n"))
|
2016-07-25 14:34:48 +00:00
|
|
|
(`value (car results)))))
|
2016-09-22 17:45:15 +00:00
|
|
|
(org-babel-result-cond (cdr (assq :result-params params))
|
2015-01-16 22:42:04 +00:00
|
|
|
result (org-babel-script-escape result)))
|
2016-09-22 17:45:15 +00:00
|
|
|
(org-babel-pick-name (cdr (assq :colname-names params))
|
|
|
|
(cdr (assq :colname-names params)))
|
|
|
|
(org-babel-pick-name (cdr (assq :rowname-names params))
|
|
|
|
(cdr (assq :rowname-names params))))))
|
2009-08-27 14:03:02 +00:00
|
|
|
|
2020-05-23 14:46:26 +00:00
|
|
|
(defun org-babel-execute:haskell (body params)
|
|
|
|
"Execute a block of Haskell code."
|
|
|
|
(let ((compile (string= "yes" (cdr (assq :compile params)))))
|
|
|
|
(if (not compile)
|
|
|
|
(org-babel-interpret-haskell body params)
|
|
|
|
(org-babel-haskell-execute body params))))
|
|
|
|
|
2015-10-26 00:56:00 +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))))
|
2015-10-29 19:26:11 +00:00
|
|
|
(org-babel--get-vars params)))
|
2010-10-21 11:03:28 +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)))
|
|
|
|
|
Backport changes from Emacs revs 115081 and 115082
2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca>
Address some byte-compiler warnings.
* ob-abc.el (org-babel-expand-body:abc): Use dolist.
(org-babel-execute:abc): Fix regexp quoting.
* ob-calc.el (org--var-syms): Rename from `var-syms'.
* ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding.
* ob-table.el (sbe): Move debug declaration.
* org-clock.el (org--msg-extra): Rename from `msg-extra'.
* org.el (org-version): Avoid var name starting with _.
(org-inhibit-startup, org-called-with-limited-levels)
(org-link-search-inhibit-query, org-time-was-given)
(org-end-time-was-given, org-def, org-defdecode, org-with-time):
* org-colview.el (org-agenda-overriding-columns-format):
* org-agenda.el (org-agenda-multi, org-depend-tag-blocked)
(org-agenda-show-log-scoped):
* ob-python.el (py-which-bufname, python-shell-buffer-name):
* ob-haskell.el (org-export-copy-to-kill-ring):
* ob-exp.el (org-link-search-inhibit-query):
* ob-R.el (ess-eval-visibly-p):
* ob-core.el (org-src-window-setup): Declare before use.
(org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'.
* ox-odt.el (org-odt-hfy-face-to-css):
* org-src.el (org-src-associate-babel-session, org-src-get-lang-mode):
* org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex)
(org-bibtex-check):
* ob-tangle.el (org-babel-tangle, org-babel-spec-to-string)
(org-babel-tangle-single-block, org-babel-tangle-comment-links):
* ob-table.el (sbe):
* ob-sqlite.el (org-babel-sqlite-expand-vars):
* ob-sql.el (org-babel-sql-expand-vars):
* ob-shen.el (org-babel-execute:shen):
* ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate):
* ob-scala.el (org-babel-scala-evaluate):
* ob-ruby.el (org-babel-ruby-table-or-string)
(org-babel-ruby-evaluate):
* ob-python.el (org-babel-python-table-or-string)
(org-babel-python-evaluate-external-process)
(org-babel-python-evaluate-session):
* ob-picolisp.el (org-babel-execute:picolisp):
* ob-perl.el (org-babel-perl-evaluate):
* ob-maxima.el (org-babel-execute:maxima):
* ob-lisp.el (org-babel-execute:lisp):
* ob-java.el (org-babel-execute:java):
* ob-io.el (org-babel-io-evaluate):
* ob-haskell.el (org-babel-execute:haskell):
* ob-fortran.el (org-babel-execute:fortran):
* ob-exp.el (org-babel-exp-code):
* ob-emacs-lisp.el (org-babel-execute:emacs-lisp):
* ob-ditaa.el (org-babel-execute:ditaa):
* ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash)
(org-babel-parse-header-arguments, org-babel-reassemble-table)
(org-babel-goto-src-block-head, org-babel-mark-block)
(org-babel-expand-noweb-references, org-babel-script-escape)
(org-babel-process-file-name):
* ob-clojure.el (org-babel-execute:clojure):
* ob-calc.el (org-babel-execute:calc):
* ob-awk.el (org-babel-execute:awk):
* ob-abc.el (org-babel-execute:abc):
* ob-R.el (org-babel-expand-body:R):
* ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...).
2013-11-12 Glenn Morris <rgm@gnu.org>
* ox-html.el (org-html-scripts): Add 2013 to copyright years.
(org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
|
|
|
(defvar org-export-copy-to-kill-ring)
|
2013-01-27 22:44:27 +00:00
|
|
|
(declare-function org-export-to-file "ox"
|
|
|
|
(backend file
|
2016-05-15 02:56:53 +00:00
|
|
|
&optional async subtreep visible-only body-only
|
|
|
|
ext-plist post-process))
|
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
|
2016-08-23 20:13:56 +00:00
|
|
|
base-name is the name of the current Org 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
|
2019-08-04 18:39:03 +00:00
|
|
|
(concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)[\r\n]"
|
2009-10-04 22:36:49 +00:00
|
|
|
"\\([^\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
|
2015-02-03 15:16:54 +00:00
|
|
|
"#+begin_export latex\n\\begin{code}\n"
|
2009-11-05 00:46:55 +00:00
|
|
|
(if (or preserve-indentp
|
|
|
|
(string-match "-i" (match-string 2)))
|
|
|
|
(match-string 3)
|
|
|
|
(org-remove-indentation (match-string 3)))
|
2015-02-03 15:16:54 +00:00
|
|
|
"\n\\end{code}\n#+end_export\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
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
;;; ob-haskell.el ends here
|