ob-screen.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ;;; ob-screen.el --- Babel Support for Interactive Terminal -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. ;; Author: Benjamin Andresen
  4. ;; Maintainer: Ken Mankoff <mankoff@gmail.com>
  5. ;; Keywords: literate programming, interactive shell
  6. ;; URL: https://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for interactive terminals. Mostly shell scripts.
  20. ;; Heavily inspired by 'eev' from Eduardo Ochs
  21. ;;
  22. ;; Adding :cmd and :terminal as header arguments
  23. ;; :terminal must support the -T (title) and -e (command) parameter
  24. ;;
  25. ;; You can test the default setup (xterm + sh) with
  26. ;; M-x org-babel-screen-test RET
  27. ;;; Code:
  28. (require 'org-macs)
  29. (org-assert-version)
  30. (require 'ob)
  31. (defvar org-babel-screen-location "screen"
  32. "The command location for screen.
  33. In case you want to use a different screen than one selected by your $PATH")
  34. (defvar org-babel-default-header-args:screen
  35. `((:results . "silent") (:session . "default") (:cmd . "sh")
  36. (:terminal . "xterm") (:screenrc . ,null-device))
  37. "Default arguments to use when running screen source blocks.")
  38. (defun org-babel-execute:screen (body params)
  39. "Send a block of code via screen to a terminal using Babel.
  40. \"default\" session is used when none is specified."
  41. (message "Sending source code block to interactive terminal session...")
  42. (save-window-excursion
  43. (let* ((session (cdr (assq :session params)))
  44. (socket (org-babel-screen-session-socketname session)))
  45. (unless socket (org-babel-prep-session:screen session params))
  46. (org-babel-screen-session-execute-string
  47. session (org-babel-expand-body:generic body params)))))
  48. (defun org-babel-prep-session:screen (_session params)
  49. "Prepare SESSION according to the header arguments specified in PARAMS."
  50. (let* ((session (cdr (assq :session params)))
  51. (cmd (cdr (assq :cmd params)))
  52. (terminal (cdr (assq :terminal params)))
  53. (screenrc (cdr (assq :screenrc params)))
  54. (process-name (concat "org-babel: terminal (" session ")")))
  55. (apply 'start-process process-name "*Messages*"
  56. terminal `("-T" ,(concat "org-babel: " session) "-e" ,org-babel-screen-location
  57. "-c" ,screenrc "-mS" ,session ,cmd))
  58. ;; XXX: Is there a better way than the following?
  59. (while (not (org-babel-screen-session-socketname session))
  60. ;; wait until screen session is available before returning
  61. )))
  62. ;; helper functions
  63. (defun org-babel-screen-session-execute-string (session body)
  64. "If SESSION exists, send BODY to it."
  65. (let ((socket (org-babel-screen-session-socketname session)))
  66. (when socket
  67. (let ((tmpfile (org-babel-screen-session-write-temp-file session body)))
  68. (apply 'start-process (concat "org-babel: screen (" session ")") "*Messages*"
  69. org-babel-screen-location
  70. `("-S" ,socket "-X" "eval" "msgwait 0"
  71. ,(concat "readreg z " tmpfile)
  72. "paste z"))))))
  73. (defun org-babel-screen-session-socketname (session)
  74. "Check if SESSION exists by parsing output of \"screen -ls\"."
  75. (let* ((screen-ls (shell-command-to-string "screen -ls"))
  76. (sockets (delq
  77. nil
  78. (mapcar
  79. (lambda (x)
  80. (when (string-match (rx (or "(Attached)" "(Detached)")) x)
  81. x))
  82. (split-string screen-ls "\n"))))
  83. (match-socket (car
  84. (delq
  85. nil
  86. (mapcar
  87. (lambda (x)
  88. (and (string-match-p (regexp-quote session) x)
  89. x))
  90. sockets)))))
  91. (when match-socket (car (split-string match-socket)))))
  92. (defun org-babel-screen-session-write-temp-file (_session body)
  93. "Save BODY in a temp file that is named after SESSION."
  94. (let ((tmpfile (org-babel-temp-file "screen-")))
  95. (with-temp-file tmpfile
  96. (insert body)
  97. (insert "\n")
  98. ;; org-babel has superfluous spaces
  99. (goto-char (point-min))
  100. (delete-matching-lines "^ +$"))
  101. tmpfile))
  102. (defun org-babel-screen-test ()
  103. "Test if the default setup works.
  104. The terminal should shortly flicker."
  105. (interactive)
  106. (let* ((random-string (format "%s" (random 99999)))
  107. (tmpfile (org-babel-temp-file "ob-screen-test-"))
  108. (body (concat "echo '" random-string "' > " tmpfile "\nexit\n"))
  109. tmp-string)
  110. (org-babel-execute:screen body org-babel-default-header-args:screen)
  111. ;; XXX: need to find a better way to do the following
  112. (while (not (file-readable-p tmpfile))
  113. ;; do something, otherwise this will be optimized away
  114. (message "org-babel-screen: File not readable yet."))
  115. (setq tmp-string (with-temp-buffer
  116. (insert-file-contents-literally tmpfile)
  117. (buffer-substring (point-min) (point-max))))
  118. (delete-file tmpfile)
  119. (message (concat "org-babel-screen: Setup "
  120. (if (string-match random-string tmp-string)
  121. "WORKS."
  122. "DOESN'T work.")))))
  123. (provide 'ob-screen)
  124. ;;; ob-screen.el ends here