Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-03-16 Thread Ihor Radchenko
Ken Mankoff  writes:

> Just a follow-up note that I am unlikely to be able to complete this patch 
> anytime soon. Re-alignment of priorities because my need for :var header 
> support in Org Babel is mitigated by a different method of injecting 
> variables into Org Babel sections: Use noweb.

Ok.
Canceled.


-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-03-15 Thread Ken Mankoff
Hi Ihor and Max,

Just a follow-up note that I am unlikely to be able to complete this patch 
anytime soon. Re-alignment of priorities because my need for :var header 
support in Org Babel is mitigated by a different method of injecting variables 
into Org Babel sections: Use noweb.

I find this more powerful than =:var=. The examples below show (1) setting a 
bash environment variable in screen, or (2) printing from a Python prompt after 
sshing to a remote computer. It is language agnostic. Because it uses 
PROPERTIES and not :var, it also lets me work in Org Column View mode.

* Header
:PROPERTIES:
:foo: 42
:END:

#+NAME: ex1-screen-bash
#+BEGIN_SRC screen
export foo="<>"
#+END_SRC

#+NAME: ex2-ssh-python
#+BEGIN_SRC bash
ssh somewhere
python
print("<>")
#+END_SRC

#+CALL: ex2-ssh-python()

#+RESULTS:
: foo


The relevant section from my library-of-babel is:

* Properties into header args
:PROPERTIES:
:hellomessage: hello
:END:

https://emacs.stackexchange.com/questions/41922/

#+NAME: get_property
#+BEGIN_SRC emacs-lisp :var prop_name="" :results silent
(org-with-point-at org-babel-current-src-block-location
  (org-entry-get nil prop_name t))
#+END_SRC

** Example Usage

*** Header arg

#+HEADER: :var prop_message=(org-entry-get nil "hellomessage" t)
#+BEGIN_SRC emacs-lisp
  (message prop_message)
#+END_SRC

#+RESULTS:
: hello

*** Noweb
#+BEGIN_SRC emacs-lisp :noweb yes
  (message "<>")
#+END_SRC

#+RESULTS:
: hello

#+BEGIN_SRC bash :noweb yes :results verbatim
echo "<>"
#+END_SRC

#+RESULTS:
: hello

If hope this helps someone if they need it.

  -k.



Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-03-02 Thread Max Nikulin

On 27/02/2023 08:59, Ken Mankoff wrote:

Have you tried to compile using make? I expect a warning due to missed
(require 'ob-shell).


Make did not complain about this, but did suggest mapcar -> mapc.


I did not expect it. However I still think, it is better to add such 
require. It should not be an error if a user is going to use ob-screen 
without explicitly loading ob-shell.



Both fixed. Should these be separate patches (3 total), or two - one for
the new feature, and one 'cleanup'?


I do not think it really matters if 2 minor fixes will be committed 
separately or as a single patch.



I noticed that ob-core besides org-babel-default-header-args:LANG uses
org-babel-header-args:LANG. The latter is not defined in ob-screen,
however I am unsure concerning its effect (completion?).


I don't understand this either.


Anyway it is unrelated to the patch we are currently discussing. It is 
an option for the future.


As to calling org-babel-variable-assignments: for specific shell, as 
Ihor suggests, it should be something like (untested)


(funcall (intern (concat "org-babel-variable-assignments:"
 (plist-get params :cmd))
 params)




Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-28 Thread Ihor Radchenko
Ken Mankoff  writes:

>> I noticed that ob-core besides org-babel-default-header-args:LANG uses
>> org-babel-header-args:LANG. The latter is not defined in ob-screen,
>> however I am unsure concerning its effect (completion?).
>
> I don't understand this either.

It defines the expected values of header arguments.
Used in completion and by org-lint.

It is a good idea to define it, but mostly for completeness.
If the header args can be anything anyway, it will not affect anything
other than the code style.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-27 Thread Ihor Radchenko
Ken Mankoff  writes:

>> To archive this, you can define a full
>> org-babel-variable-assignments:screen function that does what I
>> described.
>
> You're pushing my lisp skills here. Which is fine, but I might need some help.
>
> In ob-shell I see
>
> (defcustom org-babel-shell-names
>   '("sh" "bash" "zsh" "fish" "csh" "ash" "dash" "ksh" "mksh" "posh")
>
> Should I create a function that, using case statement or something similar, 
> checks for each of these and calls that flavor? I think I could do that.

Not each of these. AFAIU, :cmd header property in ob-screen defines
which shell to use. Knowing shell name, you can deduce the function name
to be used for variable assignments. See how it is done in
`org-babel-expand-src-block'.

> But if I also see org-babel-shell-initialize in ob-shell that looks like it 
> creates defaliases for each of these to (org-babel-execute:shell) and 
> #'org-babel-variable-assignments:shell.

The convention is for every possible #+begin_src lang to have
org-babel-variable-assignments:LANG.
ob-shell just tries to avoid unnecessary code duplication and instead of
writing identical org-babel-variable-assignments:sh
org-babel-variable-assignments:bash, org-babel-variables-assignents:zsh,
..., it generates them using a macro.

> I'm not sure how that is very different from what I've done. I'm not sure 
> what to do here, nor how to do what you suggest (yet - I've only read it so 
> far, not spent a lot of time experimenting and searching, so I may be able to 
> implement what I think is a solution to what you wrote, but I doubt it'll be 
> what you expect).

AFAIU, you don't need to juggle with macros as ob-shell does. Something
similar to `org-babel-expand-src-block' will do.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-26 Thread Ken Mankoff
Hi Max,

On 2023-02-26 at 02:11 -08, Max Nikulin  wrote:
> As to `org-babel-screen-test' perhaps the issue is additional newline
> added after random number. I have not tried stepping through the
> function in debugger though.

I haven't either. I'll see if I can figure out the issue with the help of the 
debugger.

> It seems top level headings were separated by single empty line.

Fixed.

> ;; Reuse the variable assignment code from ob-shell
>> +(defalias 'org-babel-variable-assignments:screen
>> +  'org-babel-variable-assignments:shell)
>
> Have you tried to compile using make? I expect a warning due to missed
> (require 'ob-shell).

Make did not complain about this, but did suggest mapcar -> mapc.

> P.S. My expectations based on the package name were that ob-screen is
> intended for running commands on remote hosts.

That too. But I also find it useful for running long commands on localhost 
without worrying about :async in emacs.

Another nice thing here is that this is, by default, session-based, and can be 
accessed outside of Org, while Bash blocks don't always play nice with 
sessions. I can, for example and all in Org Babel, spawn a screen session, ssh 
to a remote host, and then inject :var into the remote session. I find this 
quite powerful, and I haven't figured out how to do it with =BEGIN_SRC bash= or 
=BEGIN_SRC shell= blocks.

> Reading the code I noticed a couple of issues that may be fixed when
> you will decide to touch this file next time: -
> `org-babel-screen-session-socketname' does not respect
> `org-babel-screen-location' - `string-match' is used where it may be
> replaced by `string-match-p' since match data is not used.

Both fixed. Should these be separate patches (3 total), or two - one for the 
new feature, and one 'cleanup'?

> I noticed that ob-core besides org-babel-default-header-args:LANG uses
> org-babel-header-args:LANG. The latter is not defined in ob-screen,
> however I am unsure concerning its effect (completion?).

I don't understand this either.

  -k.



Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-26 Thread Ken Mankoff
Hi Ihor,

On 2023-02-26 at 04:18 -08, Ihor Radchenko  wrote:
>> +;; Reuse the variable assignment code from ob-shell
>> +(defalias 'org-babel-variable-assignments:screen
>> +  'org-babel-variable-assignments:shell)
>
> This will work, but you are relying on implementation detail of
> ob-shell.el. A more safe approach would be calling
> org-babel-variable-assignments:LANG depending on the :cmd header arg.
> For :cmd bash - org-babel-variable-assignments:bash, for :cmd fish -
> org-babel-variable-assignments:fish.
>
> To archive this, you can define a full
> org-babel-variable-assignments:screen function that does what I
> described.

You're pushing my lisp skills here. Which is fine, but I might need some help.

In ob-shell I see

(defcustom org-babel-shell-names
  '("sh" "bash" "zsh" "fish" "csh" "ash" "dash" "ksh" "mksh" "posh")

Should I create a function that, using case statement or something similar, 
checks for each of these and calls that flavor? I think I could do that.


But if I also see org-babel-shell-initialize in ob-shell that looks like it 
creates defaliases for each of these to (org-babel-execute:shell) and 
#'org-babel-variable-assignments:shell.

I'm not sure how that is very different from what I've done. I'm not sure what 
to do here, nor how to do what you suggest (yet - I've only read it so far, not 
spent a lot of time experimenting and searching, so I may be able to implement 
what I think is a solution to what you wrote, but I doubt it'll be what you 
expect).

  -k.



Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-26 Thread Ihor Radchenko
Ken Mankoff  writes:

> From f7ca0258d72b3da5743b1134718b1d1f0d74491d Mon Sep 17 00:00:00 2001
> From: "Kenneth D. Mankoff" 
> Date: Mon, 20 Feb 2023 21:40:39 -0800
> Subject: [PATCH] lisp/ob-screen.el: Support var header args for babel blocks

Thanks for the patch!

> +;; Reuse the variable assignment code from ob-shell
> +(defalias 'org-babel-variable-assignments:screen
> +  'org-babel-variable-assignments:shell)

This will work, but you are relying on implementation detail of
ob-shell.el. A more safe approach would be calling
org-babel-variable-assignments:LANG depending on the :cmd header arg.
For :cmd bash - org-babel-variable-assignments:bash, for :cmd fish -
org-babel-variable-assignments:fish.

To archive this, you can define a full
org-babel-variable-assignments:screen function that does what I described.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>



Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-26 Thread Max Nikulin

On 25/02/2023 23:47, Ken Mankoff wrote:

On 2023-02-25 at 08:05 -08, Max Nikulin wrote:

I believe, it is safer to define
`org-babel-variable-assignments:screen' some way: alias, substitution,
function that calls `org-babel-variable-assignments:shell'. I am
unsure which variant is better. I see a couple of callers for specific
language in ob-core.el.


How's this with a defalias?


I do not mind since ob-shell.el uses it for particular shell names.

Concerning tables as variable values, it seems sh receives them as TAB 
separated text.


As to `org-babel-screen-test' perhaps the issue is additional newline 
added after random number. I have not tried stepping through the 
function in debugger though.



diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS

...

+#+END_src
+
+
+
 * Version 9.6


It seems top level headings were separated by single empty line.


diff --git a/lisp/ob-screen.el b/lisp/ob-screen.el

...
;; Reuse the variable assignment code from ob-shell

+(defalias 'org-babel-variable-assignments:screen
+  'org-babel-variable-assignments:shell)


Have you tried to compile using make? I expect a warning due to missed 
(require 'ob-shell).


I have no objections besides this couple of minor issues. I am not a 
maintainer though.


P.S. My expectations based on the package name were that ob-screen is 
intended for running commands on remote hosts. Reading the code I 
noticed a couple of issues that may be fixed when you will decide to 
touch this file next time:
- `org-babel-screen-session-socketname' does not respect 
`org-babel-screen-location'
- `string-match' is used where it may be replaced by `string-match-p' 
since match data is not used.


I noticed that ob-core besides org-babel-default-header-args:LANG uses 
org-babel-header-args:LANG. The latter is not defined in ob-screen, 
however I am unsure concerning its effect (completion?).




Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-25 Thread Ken Mankoff
Hi Max,

On 2023-02-25 at 08:05 -08, Max Nikulin  wrote:
> I believe, it is safer to define
> `org-babel-variable-assignments:screen' some way: alias, substitution,
> function that calls `org-babel-variable-assignments:shell'. I am
> unsure which variant is better. I see a couple of callers for specific
> language in ob-core.el.

How's this with a defalias?

  -k.

>From f7ca0258d72b3da5743b1134718b1d1f0d74491d Mon Sep 17 00:00:00 2001
From: "Kenneth D. Mankoff" 
Date: Mon, 20 Feb 2023 21:40:39 -0800
Subject: [PATCH] lisp/ob-screen.el: Support var header args for babel blocks

* ob-screen.el: (org-babel-execute:screen): Parse header params
for `:var', then inject into screen session.

* etc/ORG-NEWS: Document as New Feature
---
 etc/ORG-NEWS  | 12 
 lisp/ob-screen.el |  8 
 2 files changed, 20 insertions(+)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 87ecd77cd..0da686354 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -106,6 +106,18 @@ selection.
 TODO state, priority, tags, statistics cookies, and COMMENT keywords
 are allowed in the tree structure.
 
+*** ob-screen now supports :var header arguments
+
+The ~:var~ header arg is now supported.
+
+#+BEGIN_src org
+,#+BEGIN_SRC screen :var x=42
+,echo $x
+,#+END_SRC
+#+END_src
+
+
+
 * Version 9.6
 
 ** Important announcements and breaking changes
diff --git a/lisp/ob-screen.el b/lisp/ob-screen.el
index 269538e79..6e6a31ea6 100644
--- a/lisp/ob-screen.el
+++ b/lisp/ob-screen.el
@@ -40,6 +40,10 @@
 
 (require 'ob)
 
+;; Reuse the variable assignment code from ob-shell
+(defalias 'org-babel-variable-assignments:screen
+  'org-babel-variable-assignments:shell)
+
 (defvar org-babel-screen-location "screen"
   "The command location for screen.
 In case you want to use a different screen than one selected by your $PATH")
@@ -55,8 +59,12 @@ In case you want to use a different screen than one selected by your $PATH")
   (message "Sending source code block to interactive terminal session...")
   (save-window-excursion
 (let* ((session (cdr (assq :session params)))
+   (var-lines (org-babel-variable-assignments:screen params))
(socket (org-babel-screen-session-socketname session)))
   (unless socket (org-babel-prep-session:screen session params))
+  (mapcar (lambda (var)
+(org-babel-screen-session-execute-string session var))
+  var-lines)
   (org-babel-screen-session-execute-string
session (org-babel-expand-body:generic body params)
 
-- 
2.34.1



Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-25 Thread Max Nikulin

On 25/02/2023 22:14, Ken Mankoff wrote:

On 2023-02-24 at 19:51 -08, Max Nikulin wrote:

The code still depends on on ob-shell. Is there a reason why calling
`org-babel-variable-assignments:shell' is a worse variant than copy of
the whole function body?


I didn't realize that was a dependency. I now use that function. This
also means that arrays/tables/etc. are all supported by :var, although
not in the default screen ":cmd sh" because sh doesn't support
"declare", but it works with ":cmd bash".



+   (var-lines (org-babel-variable-assignments:shell params))
(socket (org-babel-screen-session-socketname session)))


I believe, it is safer to define `org-babel-variable-assignments:screen' 
some way: alias, substitution, function that calls 
`org-babel-variable-assignments:shell'. I am unsure which variant is 
better. I see a couple of callers for specific language in ob-core.el.


I can not suggest anything specific concerning sh vs. bash. I have never 
tried to pass tables to sh src blocks.





Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-25 Thread Ken Mankoff
I note that `org-babel-screen-test` fails, but it appears to fail before
this change too...

  -k.

On Sat, Feb 25, 2023 at 7:14 AM Ken Mankoff  wrote:

> Hi Max,
>
> On 2023-02-24 at 19:51 -08, Max Nikulin  wrote:
> > On 25/02/2023 01:33, Ken Mankoff wrote:
> > I am unsure if Org markup is suitable for commit messages (~:var~)
> > since it may appear in Emacs commit logs.
>
> Fixed.
>
> > The code still depends on on ob-shell. Is there a reason why calling
> > `org-babel-variable-assignments:shell' is a worse variant than copy of
> > the whole function body?
>
> I didn't realize that was a dependency. I now use that function. This also
> means that arrays/tables/etc. are all supported by :var, although not in
> the default screen ":cmd sh" because sh doesn't support "declare", but it
> works with ":cmd bash".
>
> Improved patch attached.
>
> Thanks,
>
>   -k.
>
>
>
>


Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-25 Thread Ken Mankoff
Hi Max,

On 2023-02-24 at 19:51 -08, Max Nikulin  wrote:
> On 25/02/2023 01:33, Ken Mankoff wrote:
> I am unsure if Org markup is suitable for commit messages (~:var~)
> since it may appear in Emacs commit logs.

Fixed.

> The code still depends on on ob-shell. Is there a reason why calling
> `org-babel-variable-assignments:shell' is a worse variant than copy of
> the whole function body?

I didn't realize that was a dependency. I now use that function. This also 
means that arrays/tables/etc. are all supported by :var, although not in the 
default screen ":cmd sh" because sh doesn't support "declare", but it works 
with ":cmd bash".

Improved patch attached.

Thanks,

  -k.



>From 580499925644cdb9c2dd8c783ec03c095bec7a86 Mon Sep 17 00:00:00 2001
From: "Kenneth D. Mankoff" 
Date: Mon, 20 Feb 2023 21:40:39 -0800
Subject: [PATCH] lisp/ob-screen.el: Support var header args for babel blocks

* ob-screen.el: (org-babel-execute:screen): Parse header params
for `:var', then inject into screen session.

* etc/ORG-NEWS: Document as New Feature
---
 etc/ORG-NEWS  | 12 
 lisp/ob-screen.el |  4 
 2 files changed, 16 insertions(+)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 87ecd77cd..0da686354 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -106,6 +106,18 @@ selection.
 TODO state, priority, tags, statistics cookies, and COMMENT keywords
 are allowed in the tree structure.
 
+*** ob-screen now supports :var header arguments
+
+The ~:var~ header arg is now supported.
+
+#+BEGIN_src org
+,#+BEGIN_SRC screen :var x=42
+,echo $x
+,#+END_SRC
+#+END_src
+
+
+
 * Version 9.6
 
 ** Important announcements and breaking changes
diff --git a/lisp/ob-screen.el b/lisp/ob-screen.el
index 269538e79..bd8650f6b 100644
--- a/lisp/ob-screen.el
+++ b/lisp/ob-screen.el
@@ -55,8 +55,12 @@ In case you want to use a different screen than one selected by your $PATH")
   (message "Sending source code block to interactive terminal session...")
   (save-window-excursion
 (let* ((session (cdr (assq :session params)))
+   (var-lines (org-babel-variable-assignments:shell params))
(socket (org-babel-screen-session-socketname session)))
   (unless socket (org-babel-prep-session:screen session params))
+  (mapcar (lambda (var)
+(org-babel-screen-session-execute-string session var))
+  var-lines)
   (org-babel-screen-session-execute-string
session (org-babel-expand-body:generic body params)
 
-- 
2.34.1



Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-24 Thread Max Nikulin

On 25/02/2023 01:33, Ken Mankoff wrote:

Subject: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel
 blocks


Thank you for the patch. I suppose, suggestion to send patches as 
attachments is added mostly to prevent patches inside HTML message body. 
If a message could be feed to "git am" then it should be OK.


I am unsure if Org markup is suitable for commit messages (~:var~) since 
it may appear in Emacs commit logs.



+++ b/lisp/ob-screen.el

...

+(defun org-babel-variable-assignments:screen (params)
+  "Return list of shell statements assigning the block's variables."
+  ;; From ob-shell but change function name from ":shell" to ":screen"

...

+   (if (string-suffix-p "bash" shell-file-name)
+  (org-babel--variable-assignments:bash


The code still depends on on ob-shell. Is there a reason why calling 
`org-babel-variable-assignments:shell' is a worse variant than copy of 
the whole function body?




Re: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-24 Thread Ken Mankoff

I realize the patch should be an attachment, not the body of the email. Sorry. 
R-sending as attachment.

  -k.

>From 5a4707cc16fb5f10cd394762f41d50d8830db240 Mon Sep 17 00:00:00 2001
From: "Kenneth D. Mankoff" 
Date: Mon, 20 Feb 2023 21:40:39 -0800
Subject: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel
 blocks

* ob-screen.el: (org-babel-execute:screen): Parse header params
for `:var', then inject into screen session.
(org-babel-variable-assignments:screen): Copied from
ob-shell.el org-babel-variable-assignments:shell

* etc/ORG-NEWS: Document as New Feature

This change only supports single-value variables
(e.g., ~:var x=42~), not tables or arrays as is
currently supported by some other languages.
---
 etc/ORG-NEWS  | 13 +
 lisp/ob-screen.el | 21 +
 2 files changed, 34 insertions(+)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 87ecd77cd..d4b454d13 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -106,6 +106,19 @@ selection.
 TODO state, priority, tags, statistics cookies, and COMMENT keywords
 are allowed in the tree structure.
 
+*** ob-screen now supports :var header arguments
+
+The ~:var~ header arg now supports simple single-value variables
+(arrays and tables are not supported).
+
+#+BEGIN_src org
+,#+BEGIN_SRC screen :var x=42
+,echo $x
+,#+END_SRC
+#+END_src
+
+
+
 * Version 9.6
 
 ** Important announcements and breaking changes
diff --git a/lisp/ob-screen.el b/lisp/ob-screen.el
index 269538e79..d8f361e50 100644
--- a/lisp/ob-screen.el
+++ b/lisp/ob-screen.el
@@ -55,11 +55,32 @@ In case you want to use a different screen than one selected by your $PATH")
   (message "Sending source code block to interactive terminal session...")
   (save-window-excursion
 (let* ((session (cdr (assq :session params)))
+   (var-lines (org-babel-variable-assignments:screen params))
(socket (org-babel-screen-session-socketname session)))
   (unless socket (org-babel-prep-session:screen session params))
+  (mapcar (lambda (var)
+(org-babel-screen-session-execute-string session var))
+  var-lines)
   (org-babel-screen-session-execute-string
session (org-babel-expand-body:generic body params)
 
+(defun org-babel-variable-assignments:screen (params)
+  "Return list of shell statements assigning the block's variables."
+  ;; From ob-shell but change function name from ":shell" to ":screen"
+  (let ((sep (cdr (assq :separator params)))
+	(hline (when (string= "yes" (cdr (assq :hlines params)))
+		 (or (cdr (assq :hline-string params))
+		 "hline"
+(mapcar
+ (lambda (pair)
+   (if (string-suffix-p "bash" shell-file-name)
+	   (org-babel--variable-assignments:bash
+(car pair) (cdr pair) sep hline)
+ (org-babel--variable-assignments:sh-generic
+	  (car pair) (cdr pair) sep hline)))
+ (org-babel--get-vars params
+
+
 (defun org-babel-prep-session:screen (_session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."
   (let* ((session (cdr (assq :session params)))
-- 
2.34.1



[PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks

2023-02-24 Thread Ken Mankoff


* ob-screen.el: (org-babel-execute:screen): Parse header params
for `:var', then inject into screen session.
(org-babel-variable-assignments:screen): Copied from
ob-shell.el org-babel-variable-assignments:shell

* etc/ORG-NEWS: Document as New Feature

This change only supports single-value variables
(e.g., ~:var x=42~), not tables or arrays as is
currently supported by some other languages.

---
 etc/ORG-NEWS  | 13 +
 lisp/ob-screen.el | 21 +
 2 files changed, 34 insertions(+)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 87ecd77cd..d4b454d13 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -106,6 +106,19 @@ selection.
 TODO state, priority, tags, statistics cookies, and COMMENT keywords
 are allowed in the tree structure.
 
+*** ob-screen now supports :var header arguments
+
+The ~:var~ header arg now supports simple single-value variables
+(arrays and tables are not supported).
+
+#+BEGIN_src org
+,#+BEGIN_SRC screen :var x=42
+,echo $x
+,#+END_SRC
+#+END_src
+
+
+
 * Version 9.6
 
 ** Important announcements and breaking changes
diff --git a/lisp/ob-screen.el b/lisp/ob-screen.el
index 269538e79..d8f361e50 100644
--- a/lisp/ob-screen.el
+++ b/lisp/ob-screen.el
@@ -55,11 +55,32 @@ In case you want to use a different screen than one 
selected by your $PATH")
   (message "Sending source code block to interactive terminal session...")
   (save-window-excursion
 (let* ((session (cdr (assq :session params)))
+   (var-lines (org-babel-variable-assignments:screen params))
(socket (org-babel-screen-session-socketname session)))
   (unless socket (org-babel-prep-session:screen session params))
+  (mapcar (lambda (var)
+(org-babel-screen-session-execute-string session var))
+  var-lines)
   (org-babel-screen-session-execute-string
session (org-babel-expand-body:generic body params)
 
+(defun org-babel-variable-assignments:screen (params)
+  "Return list of shell statements assigning the block's variables."
+  ;; From ob-shell but change function name from ":shell" to ":screen"
+  (let ((sep (cdr (assq :separator params)))
+   (hline (when (string= "yes" (cdr (assq :hlines params)))
+(or (cdr (assq :hline-string params))
+"hline"
+(mapcar
+ (lambda (pair)
+   (if (string-suffix-p "bash" shell-file-name)
+  (org-babel--variable-assignments:bash
+(car pair) (cdr pair) sep hline)
+ (org-babel--variable-assignments:sh-generic
+ (car pair) (cdr pair) sep hline)))
+ (org-babel--get-vars params
+
+
 (defun org-babel-prep-session:screen (_session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."
   (let* ((session (cdr (assq :session params)))