Ihor Radchenko <yanta...@posteo.net> writes:

> Hunter Jozwiak <hunter.t....@gmail.com> writes:
>
>>>> +             (host (cdr (assq :host params)))
>>>> +             (port (cdr (assq :port params)))
>>>
>>> Please declare these new scheme-specific header argument in
>>> `org-babel-header-args:scheme'.
>>> See `org-babel-header-args:C' for an example.
>>> This is necessary for header argument completion to work.
>
>> Okay. Is there a way to tighten these inputs further? I have them set to
>> :any for the moment, but I wonder if there is a way to leverage the
>> values for `geiser-repl-default-host` and `geiser-repl-default-port`.
>
> Org-mode currently does not provide completion for header argument
> values. (see `pcomplete/org-mode/block-option/src')
>
> If Org were to support such completion, you could theoretically allow
> special values for :host/:port like default:
>
> #+begin_src scheme :host default :port default
> ...
> #+end_src
>
> to use `geiser-repl-default-host'/`...-port'.
>
> Then, you could add these values as (default :any).
>
>>>> -         (and (not (string= session "none")) session)))) ; session
>>>> +         (and (not (string= session "none")) session)  host port))) ; 
>>>> session
>>>
>>> This does not look right. Your change will disable session support
>>> completely when host and port are not provided.
>> Is there a way to test this and pinpoint the problem?
>
> Hmm. I just realized that I am reading that line wrongly - host and port
> are additional arguments passed to
> `org-babel-scheme-execute-with-geiser', not a part of
> (and (not ...) host port)
>
> So, my only comment remaining is to put each argument in its own line,
> like it is done for all the previous arguments:
>
> (org-babel-scheme-execute-with-geiser
>              full-body                       ; code
>              (string= result-type "output")  ; output?
>              impl                            ; implementation
>              (and (not (string= session "none")) session) ; session
>                host ; repl host name
>                port) ; repl port
>
> That will make the code more readable.

Here is an updated patch.

From ff82afda9b862a7899abf10b7d1a4cde3c1d5314 Mon Sep 17 00:00:00 2001
From: Hunter Jozwiak <hunter.t....@gmail.com>
Date: Sun, 28 Jan 2024 21:48:05 -0500
Subject: [PATCH] org-mode: allow ob-scheme to accept a remote connection.

* lisp/org/ob-scheme.el (org-babel-scheme-get-repl): introduce two
optional variables  host  and port. If there are not given, just run
Geiser as before. In the case when  both are given, connect to the
remotely running Scheme process.
* lisp/org/ob-scheme (org-babel-scheme-execute-with-geiser,
org-babel-execute:scheme): take these  optional arguments into account.
* lisp/org/ob-scheme.el (org-babel-header-args:scheme): define host
and port for completion.
---
 lisp/org/ob-scheme.el | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/lisp/org/ob-scheme.el b/lisp/org/ob-scheme.el
index d13b975084c..4a214b222eb 100644
--- a/lisp/org/ob-scheme.el
+++ b/lisp/org/ob-scheme.el
@@ -1,4 +1,4 @@
-;;; ob-scheme.el --- Babel Functions for Scheme      -*- lexical-binding: t; -*-
+;; ob-scheme.el --- Babel Functions for Scheme      -*- lexical-binding: t; -*-
 
 ;; Copyright (C) 2010-2024 Free Software Foundation, Inc.
 
@@ -54,7 +54,7 @@ geiser-debug-show-debug-p
 (defvar geiser-debug-jump-to-debug-p)  ; Defined in geiser-debug.el
 (defvar geiser-repl-use-other-window)  ; Defined in geiser-repl.el
 (defvar geiser-repl-window-allow-split)	; Defined in geiser-repl.el
-
+(declare-function geiser-connect "ext:geiser-repl" (impl &optional host port))
 (declare-function run-geiser "ext:geiser-repl" (impl))
 (declare-function geiser "ext:geiser-repl" (impl))
 (declare-function geiser-mode "ext:geiser-mode" ())
@@ -75,6 +75,9 @@ org-babel-scheme-null-to
 
 (defvar org-babel-default-header-args:scheme '()
   "Default header arguments for scheme code blocks.")
+(defconst org-babel-header-args:scheme '((host . :any)
+                                         (port . :any))
+  "Header arguments supported in  Scheme.")
 
 (defun org-babel-expand-body:scheme (body params)
   "Expand BODY according to PARAMS, return the expanded body."
@@ -116,13 +119,17 @@ org-babel-scheme-get-buffer-impl
   (with-current-buffer (set-buffer buffer)
     geiser-impl--implementation))
 
-(defun org-babel-scheme-get-repl (impl name)
-  "Switch to a scheme REPL, creating it if it doesn't exist."
+(defun org-babel-scheme-get-repl (impl name &optional host port)
+  "Switch to a scheme REPL, creating it if it doesn't exist.
+
+If the variables host and port are set, connect to the running Scheme REPL."
   (let ((buffer (org-babel-scheme-get-session-buffer name)))
     (or buffer
 	(progn
           (if (fboundp 'geiser)
-              (geiser impl)
+              (if (and host port)
+                  (geiser-connect impl host port)
+                (geiser impl))
             ;; Obsolete since Geiser 0.26.
 	    (run-geiser impl))
 	  (when name
@@ -159,7 +166,7 @@ org-babel-scheme-capture-current-message
        ,@body
        (current-message))))
 
-(defun org-babel-scheme-execute-with-geiser (code output impl repl)
+(defun org-babel-scheme-execute-with-geiser (code output impl repl  &optional host port)
   "Execute code in specified REPL.
 If the REPL doesn't exist, create it using the given scheme
 implementation.
@@ -175,7 +182,7 @@ org-babel-scheme-execute-with-geiser
       (let ((geiser-repl-window-allow-split nil)
 	    (geiser-repl-use-other-window nil))
 	(let ((repl-buffer (save-current-buffer
-			     (org-babel-scheme-get-repl impl repl))))
+			     (org-babel-scheme-get-repl impl repl host port))))
 	  (when (not (eq impl (org-babel-scheme-get-buffer-impl
 			       (current-buffer))))
 	    (message "Implementation mismatch: %s (%s) %s (%s)" impl (symbolp impl)
@@ -231,6 +238,8 @@ org-babel-execute:scheme
 		       geiser-scheme-implementation
 		       geiser-default-implementation
 		       (car geiser-active-implementations)))
+             (host (cdr (assq :host params)))
+             (port (cdr (assq :port params)))
 	     (session (org-babel-scheme-make-session-name
 		       source-buffer-name (cdr (assq :session params)) impl))
 	     (full-body (org-babel-expand-body:scheme body params))
@@ -240,7 +249,9 @@ org-babel-execute:scheme
 	       full-body		       ; code
 	       (string= result-type "output")  ; output?
 	       impl			       ; implementation
-	       (and (not (string= session "none")) session)))) ; session
+	       (and (not (string= session "none")) session) ; session
+               host ; REPL host
+               port))) ; REPL port
 	(let ((table
 	       (org-babel-reassemble-table
 		result
-- 
2.43.0

Attachment: signature.asc
Description: PGP signature

Reply via email to