0
0
Fork 1
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2024-09-30 05:07:48 +00:00

babel-haskell: variety of bug fixes

Thanks to Christopher Witte for raising these issues

* lisp/babel/langs/ob-haskell.el (org-babel-expand-body:haskell):
  replaced missing () wrapping, and now using correct variable
  expansion

  (org-babel-execute:haskell): now using
  org-babel-haskell-initiate-session to start sessions

  (org-babel-haskell-initiate-session): only starting sessions if none
  exists, also added a .25 second wait on brand new sessions to allow
  the Haskell interpreter to fire up

  (org-babel-load-session:haskell): now optionally accepts processed
  params to avoid over-execution of variable resolution

  (org-babel-prep-session:haskell):  now optionally accepts processed
  params to avoid over-execution of variable resolution, also placing
  the variable definitions directly in the session instead of loading
  them from a separate file
This commit is contained in:
Eric Schulte 2010-06-28 09:55:09 -07:00
parent 0e636dc2ed
commit f4b78ad3c8

View file

@ -54,10 +54,12 @@
(defun org-babel-expand-body:haskell (body params &optional processed-params) (defun org-babel-expand-body:haskell (body params &optional processed-params)
"Expand BODY according to PARAMS, return the expanded body." "Expand BODY according to PARAMS, return the expanded body."
(let (vars (nth 1 (or processed-params (org-babel-process-params params)))) (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
(concat (concat
(mapconcat (mapconcat
(lambda (pair) (format "let %s = %s;" (car pair) (cdr pair))) (lambda (pair) (format "let %s = %s"
(car pair)
(org-babel-haskell-var-to-haskell (cdr pair))))
vars "\n") "\n" body "\n"))) vars "\n") "\n" body "\n")))
(defun org-babel-execute:haskell (body params) (defun org-babel-execute:haskell (body params)
@ -68,7 +70,7 @@
(vars (nth 1 processed-params)) (vars (nth 1 processed-params))
(result-type (nth 3 processed-params)) (result-type (nth 3 processed-params))
(full-body (org-babel-expand-body:haskell body params processed-params)) (full-body (org-babel-expand-body:haskell body params processed-params))
(session (org-babel-prep-session:haskell session params)) (session (org-babel-haskell-initiate-session session params))
(raw (org-babel-comint-with-output (raw (org-babel-comint-with-output
(session org-babel-haskell-eoe t full-body) (session org-babel-haskell-eoe t full-body)
(insert (org-babel-trim full-body)) (insert (org-babel-trim full-body))
@ -96,34 +98,35 @@
"If there is not a current inferior-process-buffer in SESSION "If there is not a current inferior-process-buffer in SESSION
then create one. Return the initialized session." then create one. Return the initialized session."
;; TODO: make it possible to have multiple sessions ;; TODO: make it possible to have multiple sessions
(run-haskell) (current-buffer)) (or (get-buffer "*haskell*")
(save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
(defun org-babel-load-session:haskell (session body params) (defun org-babel-load-session:haskell
(session body params &optional processed-params)
"Load BODY into SESSION." "Load BODY into SESSION."
(save-window-excursion (save-window-excursion
(let* ((buffer (org-babel-prep-session:haskell session params)) (let* ((buffer (org-babel-prep-session:haskell
session params processed-params))
(load-file (concat (make-temp-file "org-babel-haskell-load") ".hs"))) (load-file (concat (make-temp-file "org-babel-haskell-load") ".hs")))
(with-temp-buffer (with-temp-buffer
(insert body) (write-file load-file) (insert body) (write-file load-file)
(haskell-mode) (inferior-haskell-load-file)) (haskell-mode) (inferior-haskell-load-file))
buffer))) buffer)))
(defun org-babel-prep-session:haskell (session params) (defun org-babel-prep-session:haskell
(session params &optional processesed-params)
"Prepare SESSION according to the header arguments specified in PARAMS." "Prepare SESSION according to the header arguments specified in PARAMS."
(save-window-excursion (save-window-excursion
(org-babel-haskell-initiate-session session) (let ((pp (or processed-params (org-babel-process-params params)))
(let* ((vars (org-babel-ref-variables params)) (buffer (org-babel-haskell-initiate-session session)))
(var-lines (mapconcat ;; define any variables (org-babel-comint-in-buffer buffer
(mapcar
(lambda (pair) (lambda (pair)
(format "%s=%s" (insert (format "let %s = %s"
(car pair) (car pair)
(org-babel-ruby-var-to-ruby (cdr pair)))) (org-babel-haskell-var-to-haskell (cdr pair))))
vars "\n")) (comint-send-input nil t))
(vars-file (concat (make-temp-file "org-babel-haskell-vars") ".hs"))) (nth 1 pp)))
(when vars
(with-temp-buffer
(insert var-lines) (write-file vars-file)
(haskell-mode) (inferior-haskell-load-file)))
(current-buffer)))) (current-buffer))))
(defun org-babel-haskell-table-or-string (results) (defun org-babel-haskell-table-or-string (results)
@ -140,6 +143,13 @@ Emacs-lisp table, otherwise return the results as a string."
"'" "\"" results)))))) "'" "\"" results))))))
results))) results)))
(defun org-babel-haskell-var-to-haskell (var)
"Convert an elisp var into a string of haskell source code
specifying a var of the same value."
(if (listp var)
(concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var ", ") "]")
(format "%S" var)))
(defun org-babel-haskell-export-to-lhs (&optional arg) (defun org-babel-haskell-export-to-lhs (&optional arg)
"Export to a .lhs file with all haskell code blocks escaped "Export to a .lhs file with all haskell code blocks escaped
appropriately. When called with a prefix argument the resulting appropriately. When called with a prefix argument the resulting