emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ken Mankoff <mankoff@gmail.com>
To: Org-mode <emacs-orgmode@gnu.org>
Subject: [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks
Date: Fri, 24 Feb 2023 09:03:18 -0800	[thread overview]
Message-ID: <87pm9z8089.fsf@t480.home> (raw)


* 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)))


             reply	other threads:[~2023-02-24 17:04 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-24 17:03 Ken Mankoff [this message]
2023-02-24 18:33 ` [PATCH] lisp/ob-screen.el: Support ~:var~ header args for babel blocks Ken Mankoff
2023-02-25  3:51   ` Max Nikulin
2023-02-25 15:14     ` Ken Mankoff
2023-02-25 15:19       ` Ken Mankoff
2023-02-25 16:05       ` Max Nikulin
2023-02-25 16:47         ` Ken Mankoff
2023-02-26 10:11           ` Max Nikulin
2023-02-27  1:59             ` Ken Mankoff
2023-02-28 10:40               ` Ihor Radchenko
2023-03-16  4:12                 ` Ken Mankoff
2023-03-16 10:22                   ` Ihor Radchenko
2023-03-16 15:09                     ` [PATCH v4] " Max Nikulin
2023-03-17 11:41                       ` Max Nikulin
2023-03-18 12:08                       ` Ihor Radchenko
2023-03-19 14:42                       ` Ken Mankoff
2023-03-21 14:12                         ` Max Nikulin
2023-03-02 13:38               ` [PATCH] " Max Nikulin
2023-02-26 12:18           ` Ihor Radchenko
2023-02-27  1:59             ` Ken Mankoff
2023-02-27 19:43               ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87pm9z8089.fsf@t480.home \
    --to=mankoff@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).