From b3dbcc39ab119162a9a0c5a172a33f6367a13845 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Sat, 25 Mar 2023 10:06:44 +0100 Subject: [PATCH 6/7] ob-haskell: Implement sessions * lisp/ob-haskell.el (org-babel-haskell-initiate-session): Implement sessions. * testing/lisp/test-ob-haskell-ghci.el: Update tests related to sessions. --- lisp/ob-haskell.el | 50 +++++++++++++++++++++------- testing/lisp/test-ob-haskell-ghci.el | 8 +++-- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/lisp/ob-haskell.el b/lisp/ob-haskell.el index a8a8df9d8..f7acb1011 100644 --- a/lisp/ob-haskell.el +++ b/lisp/ob-haskell.el @@ -51,6 +51,8 @@ (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)) +(declare-function inferior-haskell-start-process + "ext:inf-haskell" ()) (declare-function org-entry-get "org" (pom property &optional inherit literal-nil)) (defvar org-babel-tangle-lang-exts) @@ -183,20 +185,44 @@ (defun org-babel-execute:haskell (body params) (org-babel-interpret-haskell body params) (org-babel-haskell-execute body params)))) -(defun org-babel-haskell-initiate-session (&optional _session _params) + + + +;; Variable defined in inf-haskell (haskell-mode package). +(defvar inferior-haskell-buffer) + +(defun org-babel-haskell-initiate-session (&optional session-name _params) "Initiate a haskell session. -If there is not a current inferior-process-buffer in SESSION -then create one. Return the initialized session." +Return the initialized session." (org-require-package 'inf-haskell "haskell-mode") - (or (get-buffer "*haskell*") - (save-window-excursion - (run-haskell) - (sleep-for 0.25) - ;; Disable secondary prompt. - (org-babel-comint-input-command - (current-buffer) - ":set prompt-cont \"\"") - (current-buffer)))) + (when (and session-name (string= session-name "none")) + (setq session-name nil)) + (unless session-name + ;; As haskell-mode is using the buffer name "*haskell*", we stay + ;; away from it. + (setq session-name (generate-new-buffer-name "*ob-haskell*"))) + (let ((session (get-buffer session-name))) + (save-window-excursion + (or (org-babel-comint-buffer-livep session) + (let ((inferior-haskell-buffer session)) + (when (and (bufferp session) (not (org-babel-comint-buffer-livep session))) + (when (bufferp "*haskell*") (error "Conflicting buffer '*haskell*', rename it or kill it.")) + (with-current-buffer session (rename-buffer "*haskell*"))) + (save-window-excursion + ;; We don't use `run-haskell' to not popup the buffer. + ;; And we protect default-directory. + (let ((default-directory default-directory)) + (inferior-haskell-start-process)) + (sleep-for 0.25) + (setq session inferior-haskell-buffer) + (with-current-buffer session (rename-buffer session-name)) + ;; Disable secondary prompt. + (org-babel-comint-input-command + session + ":set prompt-cont \"\"") + session)))) + session)) + (defun org-babel-load-session:haskell (session body params) "Load BODY into SESSION." diff --git a/testing/lisp/test-ob-haskell-ghci.el b/testing/lisp/test-ob-haskell-ghci.el index 2ddfb4de7..907487960 100644 --- a/testing/lisp/test-ob-haskell-ghci.el +++ b/testing/lisp/test-ob-haskell-ghci.el @@ -133,7 +133,6 @@ (ert-deftest ob-haskell/hello-world-output-multilines () (ert-deftest ob-haskell/sessions-must-not-share-variables () "Sessions must not share variables." - :expected-result :failed (test-ob-haskell-ghci-with-global-session (test-ob-haskell-ghci ":session s1" "x=2" nil :unprotected) (should (equal 2 (test-ob-haskell-ghci ":session s1" "x" nil :unprotected))) @@ -143,11 +142,16 @@ (ert-deftest ob-haskell/sessions-must-not-share-variables () (ert-deftest ob-haskell/no-session-means-one-shot-sessions () "When no session, use a new session." - :expected-result :failed (test-ob-haskell-ghci-with-global-session (test-ob-haskell-ghci "" "x=2" nil :unprotected) (should-not (equal 2 (test-ob-haskell-ghci "" "x" nil :unprotected))))) +(ert-deftest ob-haskell/reuse-variables-in-same-session () + "Reuse variables between blocks using the same session." + (test-ob-haskell-ghci ":session s1" "x=2" nil) + (should (equal 2 (test-ob-haskell-ghci ":session s1" "x")))) + + ;;;; Values ;; -- 2.39.1