1
0
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2024-11-23 07:18:53 +00:00
org-mode/lisp/ob-haskell.el
Nicolas Goaziou f0bf77e82a Activate lexical binding in some libraries
* lisp/ob-C.el (org-babel-prep-session:C):
(org-babel-load-session:C):
* lisp/ob-J.el:
(org-babel-expand-body:J):
(org-babel-execute:J):
* lisp/ob-R.el:
(org-babel-expand-body:R):
* lisp/ob-abc.el:
(org-babel-execute:abc):
(org-babel-prep-session:abc):
* lisp/ob-asymptote.el:
(org-babel-execute:asymptote):
(org-babel-prep-session:asymptote):
* lisp/ob-awk.el:
(org-babel-expand-body:awk):
* lisp/ob-calc.el:
(org-babel-expand-body:calc):
* lisp/ob-clojure.el:
* lisp/ob-comint.el:
(org-babel-comint-in-buffer):
(org-babel-comint-with-output):
(org-babel-comint-eval-invisibly-and-wait-for-file):
* lisp/ob-coq.el:
* lisp/ob-css.el:
(org-babel-execute:css):
(org-babel-prep-session:css):
* lisp/ob-ditaa.el:
(org-babel-execute:ditaa):
(org-babel-prep-session:ditaa):
* lisp/ob-dot.el:
(org-babel-execute:dot):
(org-babel-prep-session:dot):
* lisp/ob-ebnf.el:
* lisp/ob-emacs-lisp.el:
* lisp/ob-eval.el:
* lisp/ob-forth.el:
* lisp/ob-fortran.el:
(org-babel-execute:fortran):
(org-babel-prep-session:fortran):
(org-babel-load-session:fortran):
* lisp/ob-gnuplot.el:
(org-babel-expand-body:gnuplot):
(org-babel-prep-session:gnuplot):
(org-babel-gnuplot-initiate-session):
* lisp/ob-groovy.el:
(org-babel-prep-session:groovy):
(org-babel-groovy-initiate-session):
* lisp/ob-haskell.el:
(org-babel-haskell-initiate-session):
* lisp/ob-io.el:
(org-babel-prep-session:io):
(org-babel-io-initiate-session):
* lisp/ob-java.el:
(org-babel-execute:java):
* lisp/ob-js.el:
* lisp/ob-keys.el:
* lisp/ob-latex.el:
(org-babel-prep-session:latex):
* lisp/ob-ledger.el:
(org-babel-execute:ledger):
(org-babel-prep-session:ledger):
* lisp/ob-lilypond.el:
(org-babel-lilypond-commands):
(org-babel-lilypond-process-basic):
(org-babel-prep-session:lilypond):
(org-babel-lilypond-parse-line-num):
* lisp/ob-lisp.el:
* lisp/ob-makefile.el:
(org-babel-execute:makefile):
(org-babel-prep-session:makefile):
* lisp/ob-matlab.el:
* lisp/ob-maxima.el:
(org-babel-prep-session:maxima):
* lisp/ob-mscgen.el:
(org-babel-prep-session:mscgen):
* lisp/ob-ocaml.el:
(org-babel-execute:ocaml):
(org-babel-prep-session:ocaml):
* lisp/ob-octave.el:
(org-babel-execute:octave):
(org-babel-octave-initiate-session):
* lisp/ob-org.el:
(org-babel-prep-session:org):
* lisp/ob-perl.el:
(org-babel-prep-session:perl):
(org-babel-perl--var-to-perl):
(org-babel-perl-initiate-session):
* lisp/ob-picolisp.el:
(org-babel-expand-body:picolisp):
(org-babel-execute:picolisp):
* lisp/ob-plantuml.el:
(org-babel-execute:plantuml):
(org-babel-prep-session:plantuml):
* lisp/ob-processing.el:
(org-babel-prep-session:processing):
* lisp/ob-python.el:
(org-babel-python-initiate-session):
* lisp/ob-ref.el:
(org-babel-ref-resolve):
* lisp/ob-ruby.el:
(org-babel-ruby-initiate-session):
* lisp/ob-sass.el:
(org-babel-execute:sass):
(org-babel-prep-session:sass):
* lisp/ob-scala.el:
(org-babel-execute:scala):
(org-babel-prep-session:scala):
(org-babel-scala-initiate-session):
* lisp/ob-scheme.el:
* lisp/ob-screen.el:
(org-babel-prep-session:screen):
(org-babel-screen-session-write-temp-file):
(org-babel-screen-test):
* lisp/ob.el:
* lisp/org-colview.el:
(org-columns-todo):
(org-columns-set-tags-or-toggle):
(org-columns-new):
(org-columns-uncompile-format):
(org-agenda-colview-summarize):
* lisp/org-footnote.el:
(electric-indent-mode):
* lisp/org-indent.el:
(org-indent-refresh-maybe):
* lisp/org-list.el:
* lisp/org-macro.el:
(org-macro--collect-macros):
* lisp/org-src.el:
* lisp/org-table.el:
(sort-fold-case):
(org-table-create):
(org-table-field-info):
(org-table-transpose-table-at-point):
(org-table-remove-rectangle-highlight):
(orgtbl-create-or-convert-from-region):
(org-define-lookup-function):
* lisp/ox-ascii.el:
(org-ascii-format-drawer-function):
(org-ascii--has-caption-p):
(org-ascii-bold):
(org-ascii-center-block):
(org-ascii-clock):
(org-ascii-code):
(org-ascii-dynamic-block):
(org-ascii-entity):
(org-ascii-example-block):
(org-ascii-export-snippet):
(org-ascii-export-block):
(org-ascii-fixed-width):
(org-ascii-footnote-reference):
(org-ascii-horizontal-rule):
(org-ascii-inline-src-block):
(org-ascii-format-inlinetask-default):
(org-ascii-italic):
(org-ascii-keyword):
(org-ascii-latex-environment):
(org-ascii-latex-fragment):
(org-ascii-line-break):
(org-ascii-node-property):
(org-ascii-planning):
(org-ascii-quote-block):
(org-ascii-radio-target):
(org-ascii-special-block):
(org-ascii-src-block):
(org-ascii-statistics-cookie):
(org-ascii-subscript):
(org-ascii-superscript):
(org-ascii-strike-through):
(org-ascii-timestamp):
(org-ascii-underline):
(org-ascii-verbatim):
(org-ascii-verse-block):
(org-ascii-filter-headline-blank-lines):
(org-ascii-filter-paragraph-spacing):
(org-ascii-filter-comment-spacing): Use lexical binding.
2015-10-26 01:56:00 +01:00

219 lines
8.6 KiB
EmacsLisp

;;; ob-haskell.el --- Babel Functions for Haskell -*- lexical-binding: t; -*-
;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
;; Author: Eric Schulte
;; Keywords: literate programming, reproducible research
;; Homepage: http://orgmode.org
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; 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,
;; 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.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; 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
;;
;; - (optionally) lhs2tex :: http://people.cs.uu.nl/andres/lhs2tex/
;;; Code:
(require 'ob)
(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))
(defvar org-babel-tangle-lang-exts)
(add-to-list 'org-babel-tangle-lang-exts '("haskell" . "hs"))
(defvar org-babel-default-header-args:haskell
'((:padlines . "no")))
(defvar org-babel-haskell-lhs2tex-command "lhs2tex")
(defvar org-babel-haskell-eoe "\"org-babel-haskell-eoe\"")
(defun org-babel-execute:haskell (body params)
"Execute a block of Haskell code."
(let* ((session (cdr (assoc :session params)))
(result-type (cdr (assoc :result-type params)))
(full-body (org-babel-expand-body:generic
body params
(org-babel-variable-assignments:haskell params)))
(session (org-babel-haskell-initiate-session session params))
(raw (org-babel-comint-with-output
(session org-babel-haskell-eoe t full-body)
(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)))))))
(org-babel-reassemble-table
(let ((result
(case result-type
(output (mapconcat #'identity (reverse (cdr results)) "\n"))
(value (car results)))))
(org-babel-result-cond (cdr (assoc :result-params params))
result (org-babel-script-escape result)))
(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))))))
(defun org-babel-haskell-read-string (string)
"Strip \\\"s from around a haskell string."
(if (string-match "^\"\\([^\000]+\\)\"$" string)
(match-string 1 string)
string))
(defun org-babel-haskell-initiate-session (&optional _session _params)
"Initiate a haskell session.
If there is not a current inferior-process-buffer in SESSION
then create one. Return the initialized session."
(require 'inf-haskell)
(or (get-buffer "*haskell*")
(save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
(defun org-babel-load-session:haskell (session body params)
"Load BODY into SESSION."
(save-window-excursion
(let* ((buffer (org-babel-prep-session:haskell session params))
(load-file (concat (org-babel-temp-file "haskell-load-") ".hs")))
(with-temp-buffer
(insert body) (write-file load-file)
(haskell-mode) (inferior-haskell-load-file))
buffer)))
(defun org-babel-prep-session:haskell (session params)
"Prepare SESSION according to the header arguments in PARAMS."
(save-window-excursion
(let ((buffer (org-babel-haskell-initiate-session session)))
(org-babel-comint-in-buffer buffer
(mapc (lambda (line)
(insert line)
(comint-send-input nil t))
(org-babel-variable-assignments:haskell params)))
(current-buffer))))
(defun org-babel-variable-assignments:haskell (params)
"Return list of haskell statements assigning the block's variables."
(mapcar (lambda (pair)
(format "let %s = %s"
(car pair)
(org-babel-haskell-var-to-haskell (cdr pair))))
(mapcar #'cdr (org-babel-get-header params :var))))
(defun org-babel-haskell-var-to-haskell (var)
"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."
(if (listp var)
(concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var ", ") "]")
(format "%S" var)))
(defvar org-export-copy-to-kill-ring)
(declare-function org-export-to-file "ox"
(backend file
&optional async subtreep visible-only body-only ext-plist))
(defun org-babel-haskell-export-to-lhs (&optional arg)
"Export to a .lhs file with all haskell code blocks escaped.
When called with a prefix argument the resulting
.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.
Note that all standard Babel literate programming
constructs (header arguments, no-web syntax etc...) are ignored."
(interactive "P")
(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)))
(tmp-file (org-babel-temp-file "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"))
(command (concat org-babel-haskell-lhs2tex-command
" " (org-babel-process-file-name lhs-file)
" > " (org-babel-process-file-name tex-file)))
(preserve-indentp org-src-preserve-indentation)
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"
(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"))
t t)
(indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
(save-excursion
;; export to latex w/org and save as .lhs
(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))
(kill-buffer nil)
(delete-file tmp-org-file)
(find-file tmp-tex-file)
(goto-char (point-min)) (forward-line 2)
(insert "%include polycode.fmt\n")
;; 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))
(setq contents (buffer-string))
(save-buffer) (kill-buffer nil))
(delete-file tmp-tex-file)
;; save org exported latex to a .lhs file
(with-temp-file lhs-file (insert contents))
(if (not arg)
(find-file lhs-file)
;; process .lhs file with lhs2tex
(message "running %s" command) (shell-command command) (find-file tex-file))))
(provide 'ob-haskell)
;;; ob-haskell.el ends here