Hi Max,

Following up from 2023...

On 2023-03-21 at 10:12 -04, Max Nikulin <maniku...@gmail.com> wrote...
> On 19/03/2023 21:42, Ken Mankoff wrote:
>
>> +++ b/lisp/ob-screen.el
>> @@ -98,7 +98,7 @@ In case you want to use a different screen than one 
>> selected by your $PATH")
>>     (defun org-babel-screen-session-socketname (session)
>>     "Check if SESSION exists by parsing output of \"screen -ls\"."
>> -  (let* ((screen-ls (shell-command-to-string "screen -ls"))
>> +  (let* ((screen-ls (shell-command-to-string (concat 
>> org-babel-screen-location " -ls")))
>
> Currently `org-babel-screen-location' is used as an argument of
> `start-process', so if the value contains spaces or other shell
> special characters they are not interpreted by shell. Since there is
> no convenience function like process-to-string that accepts command as
> list instead of string with a shell command, it is better to pass
> screen location through `shell-quote-argument'.
>
> You may try to create a directory with a space in its name, create a
> symlink from this directory to /usr/bin/srceen, and set
> `org-babel-screen-location' to the full path (with space).

It looks like I never responded to this. I just tested with

#+BEGIN_SRC emacs-lisp
(setq org-babel-screen-location "/home/kdm/tmp/dir with space/screen")
#+END_SRC

#+BEGIN_SRC screen
ls
#+END_SRC

And it works. I'm not sure why it works, given your comment that it won't work. 
But also, I've updated the patch.

>From an earlier comment in this old thread,

> 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.

It was not the newline. Adding string-trim to:

    (setq tmp-string (string-trim (with-temp-buffer
                       (insert-file-contents-literally tmpfile)
                       (buffer-substring (point-min) (point-max)))))


Still fails. Weirdly, the fix is (sleep-for 0.1) in the test. But not in this 
loop:

;; XXX: need to find a better way to do the following
(while (not (file-readable-p tmpfile))
  (sleep-for 0.1)
  ;; do something, otherwise this will be optimized away
  (message "org-babel-screen: File not readable yet."))

Anyway, please see attached patches.

  -k.


>From e185bd24a6774d89186c2d70efeed33145754944 Mon Sep 17 00:00:00 2001
From: Ken Mankoff <mank...@gmail.com>
Date: Mon, 21 Jul 2025 10:04:01 -0400
Subject: [PATCH 1/2] Improve support custom screen location

Custom screen location was previously supported, but if screen was
located in a path with spaces in the name, it may not have
worked. Calls to screen are now wrapped in `shell-quote-argument`
---
 lisp/org/ob-screen.el | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lisp/org/ob-screen.el b/lisp/org/ob-screen.el
index 1867cdd6e1e..c9b1d8becca 100644
--- a/lisp/org/ob-screen.el
+++ b/lisp/org/ob-screen.el
@@ -68,7 +68,8 @@ org-babel-prep-session:screen
          (screenrc (cdr (assq :screenrc params)))
          (process-name (concat "org-babel: terminal (" session ")")))
     (apply 'start-process process-name "*Messages*"
-           terminal `("-T" ,(concat "org-babel: " session) "-e" ,org-babel-screen-location
+           terminal `("-T" ,(concat "org-babel: " session) "-e" ,
+                      (shell-quote-argument org-babel-screen-location)
 		      "-c" ,screenrc "-mS" ,session ,cmd))
     ;; XXX: Is there a better way than the following?
     (while (not (org-babel-screen-session-socketname session))
@@ -83,14 +84,14 @@ org-babel-screen-session-execute-string
     (when socket
       (let ((tmpfile (org-babel-screen-session-write-temp-file session body)))
         (apply 'start-process (concat "org-babel: screen (" session ")") "*Messages*"
-               org-babel-screen-location
+               (shell-quote-argument org-babel-screen-location)
                `("-S" ,socket "-X" "eval" "msgwait 0"
 		 ,(concat "readreg z " tmpfile)
 		 "paste z"))))))
 
 (defun org-babel-screen-session-socketname (session)
   "Check if SESSION exists by parsing output of \"screen -ls\"."
-  (let* ((screen-ls (shell-command-to-string "screen -ls"))
+  (let* ((screen-ls (shell-command-to-string (concat (shell-quote-argument org-babel-screen-location) " -ls")))
          (sockets (delq
 		   nil
                    (mapcar
-- 
2.47.2

>From 0a824c00600292c9517f659b085eafcfac30b105 Mon Sep 17 00:00:00 2001
From: Ken Mankoff <mank...@gmail.com>
Date: Mon, 21 Jul 2025 10:04:26 -0400
Subject: [PATCH 2/2] org-babel-screen-test now passes.

---
 lisp/org/ob-screen.el | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/lisp/org/ob-screen.el b/lisp/org/ob-screen.el
index c9b1d8becca..ee4efe182f7 100644
--- a/lisp/org/ob-screen.el
+++ b/lisp/org/ob-screen.el
@@ -131,15 +131,16 @@ org-babel-screen-test
          tmp-string)
     (org-babel-execute:screen body org-babel-default-header-args:screen)
     ;; XXX: need to find a better way to do the following
-    (while (not (file-readable-p tmpfile))
-      ;; do something, otherwise this will be optimized away
-      (message "org-babel-screen: File not readable yet."))
+    ;; (while (not (file-readable-p tmpfile))
+    ;;   ;; do something, otherwise this will be optimized away
+    ;;   (message "org-babel-screen: File not readable yet."))
+    (sleep-for 0.1) ;; Putting this in above (while) does not work?
     (setq tmp-string (with-temp-buffer
                        (insert-file-contents-literally tmpfile)
                        (buffer-substring (point-min) (point-max))))
     (delete-file tmpfile)
     (message (concat "org-babel-screen: Setup "
-                     (if (string-match random-string tmp-string)
+                     (if (string-match-p random-string tmp-string)
                          "WORKS."
 		       "DOESN'T work.")))))
 
-- 
2.47.2

Reply via email to