emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ob-shell sessions will send final echo '...' as input key sequence w


From: Christopher M. Miles
Subject: Re: ob-shell sessions will send final echo '...' as input key sequence when previous command reads input interactively
Date: Wed, 03 May 2023 19:01:18 +0800
User-agent: mu4e 1.10.3; emacs 30.0.50

Ihor Radchenko <yantar92@posteo.net> writes:

> Matt <matt@excalamus.com> writes:
>
>>  > Matt, maybe you have some ideas about this edge case?
>>
>> I have no other ideas within the current ob-shell implementation.  As for 
>> modifications, I have the following thoughts.
>>
>> First, how might we state the problem for this edge case?
>>
>> To me, it looks like, "How can the ob-shell :async option manage interactive 
>> input?" Do you agree
>> with this formulation? If not, how do you see it differently?
>
> It is not about :async per se. Rather about using :session.
>
> A simple reproducer is
>
> #+begin_src bash :session test
> mpv "/path/to/some/video"
> m_
> #+end_src
>
> Basically, when you send multiline command in comint buffer without
> waiting for each line to finish, the following line may be read by the
> running command if that command is interactive.
>
> It is similar to a situation when you run a normal terminal like
>
> $ mpv "..."
> <now type "m_" while mpv is still running>
> <exit mpv>
> $ m_ <m_ is echoed in the next prompt>
>

Indeed, this explaination give me more clear understanding of the problem.

So currently ob-shell.el is async sending command into session buffer
without waiting for command to be finished.

I checked out the ob-shell.el source code. I suppose this is the core
part of the problem. From intuitive view, session async evaluation
indeed should not wait for command to be finished. But still feel a
little weird.

#+begin_src emacs-lisp
(session                        ; session evaluation
            (if async
                (progn
                  (let ((uuid (org-id-uuid)))
                    (org-babel-comint-async-register
                     session
                     (current-buffer)
                     "ob_comint_async_shell_\\(.+\\)_\\(.+\\)"
                     'ob-shell-async-chunk-callback
                     nil)
                    (org-babel-comint-async-delete-dangling-and-eval
                        session
                      (insert (format ob-shell-async-indicator "start" uuid))
                      (comint-send-input nil t)
                      (insert (org-trim body))
                      (comint-send-input nil t)
                      (insert (format ob-shell-async-indicator "end" uuid))
                      (comint-send-input nil t))
                    uuid))
              (mapconcat
               #'org-babel-sh-strip-weird-long-prompt
               (mapcar
                #'org-trim
                (butlast ; Remove eoe indicator
                 (org-babel-comint-with-output
                     (session org-babel-sh-eoe-output t body)
                   (insert (org-trim body) "\n"
                           org-babel-sh-eoe-indicator)
                   (comint-send-input nil t))
                 ;; Remove `org-babel-sh-eoe-indicator' output line.
                 1))
               "\n")))
#+end_src

>> One thought is to update :async to work with the :stdin option so that
>> the block is run as a script. Currently, :stdin runs synchronously in
>> a separate shell. We might be able to grab the script's output and put
>> it into the session buffer. See how the following runs in a temporary
>> shell, regardless of the :session/:async options.
>
> Avoiding session altogether will indeed solve the problem, as :session
> is the place where the problem lies.
>
> Adding :stdin support is also an interesting idea, even out of scope of
> this discussion.

If this :stdin interesting idea works, maybe other similar ideas will too.🙋

For examples:

- Wrap command "mpv" with a shell function which disable accepting interactive 
input.
- Setting shell or environment variable in :prolog for source block to 
preventing interactive input.

Anyway, glad to get more interesting ideas.

-- 

[ stardiviner ]
I try to make every word tell the meaning that I want to express without 
misunderstanding.

Blog: https://stardiviner.github.io/
IRC(libera.chat, freenode): stardiviner, Matrix: stardiviner
GPG: F09F650D7D674819892591401B5DF1C95AE89AC3

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]