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: Mon, 01 May 2023 21:41:49 +0800
User-agent: mu4e 1.10.3; emacs 30.0.50

Ihor Radchenko <yantar92@posteo.net> writes:

> "Christopher M. Miles" <numbchild@gmail.com> writes:
>
>> I might consider to use another language like python instead of sh to
>> execute command to play video to get around this issue. I will update
>> here if I success.
>
> All you need to do is getting rid of :async and :session.
> Just use :results none + "&" at the end of mpv command.
> Non-session blocks will not suffer from this problem.

My first version property "EVAL" inline src block indeed don't using
:async and :session. It can play video file.

But I found it will block [Tab] org-cycle expand headline. I don't want
Emacs to be suspended. So I added :async change.

Now I use python `os.system()` to invoke shell command works fine.

Here is my current version:

(defcustom org-property-eval-keyword "EVAL"
  "A property keyword for evaluate code."
  :type 'string
  :safe #'stringp
  :group 'org)

(add-to-list 'org-default-properties org-property-eval-keyword)

(defun org-property-eval-on-cycle-expand (&optional state)
  "Evaluate Org inline source block in property value on headline cycle expand."
  (when (memq state '(children subtree))
    (if-let ((inline-src-block (org-entry-get nil org-property-eval-keyword 
nil)))
        (with-temp-buffer
          (insert inline-src-block)
          (goto-char (point-min))
          (let* ((context (org-element-context))
                 (lang (org-element-property :language context))
                 (type (org-element-type context))
                 (src-block-info (org-babel-get-src-block-info nil context)))
            (when (eq type 'inline-src-block)
              (org-babel-execute-src-block
               nil src-block-info
               (pcase lang
                 ("sh" `((:session . ,(make-temp-name " *ob-sh-inline-async 
(sh) ")) (:async . "yes") (:results . "silent")))
                 ("shell" `((:session . ,(make-temp-name " *ob-sh-inline-async 
(shell) ")) (:async . "yes") (:results . "silent")))
                 ("bash" `((:session . ,(make-temp-name " *ob-sh-inline-async 
(bash) ")) (:async . "yes") (:results . "silent")))
                 ("zsh" `((:session . ,(make-temp-name " *ob-sh-inline-async 
(zsh) ")) (:async . "yes") (:results . "silent")))
                 ("python" `((:session . ,(make-temp-name 
"ob-python-inline-async ")) (:async . "yes") (:results . "silent")))
                 (_ '((:results . "none")))))))))))

(add-hook 'org-cycle-hook #'org-property-eval-on-cycle-expand)

(defvar org-property-eval-templates
  '("src_sh{mpg123 \"path/to/music.mp3\"}"
    "src_python{import os; os.system(\"mpv 'path/to/video.mp4' \")}"))
(org-completing-read-property-construct-function "EVAL" 
org-property-eval-templates)
(add-to-list 'org-property-set-functions-alist '("EVAL" . 
org-completing-read-property_eval))

-- 

[ 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]