ob-screen.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ;;; ob-screen.el --- Babel Support for Interactive Terminal -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2021 Free Software Foundation, Inc.
  3. ;; Author: Benjamin Andresen
  4. ;; Maintainer: Ken Mankoff
  5. ;; Keywords: literate programming, interactive shell
  6. ;; Homepage: 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 'ob)
  29. (defvar org-babel-screen-location "screen"
  30. "The command location for screen.
  31. In case you want to use a different screen than one selected by your $PATH")
  32. (defvar org-babel-default-header-args:screen
  33. `((:results . "silent") (:session . "default") (:cmd . "sh")
  34. (:terminal . "xterm") (:screenrc . ,null-device))
  35. "Default arguments to use when running screen source blocks.")
  36. (defun org-babel-execute:screen (body params)
  37. "Send a block of code via screen to a terminal using Babel.
  38. \"default\" session is used when none is specified."
  39. (message "Sending source code block to interactive terminal session...")
  40. (save-window-excursion
  41. (let* ((session (cdr (assq :session params)))
  42. (socket (org-babel-screen-session-socketname session)))
  43. (unless socket (org-babel-prep-session:screen session params))
  44. (org-babel-screen-session-execute-string
  45. session (org-babel-expand-body:generic body params)))))
  46. (defun org-babel-prep-session:screen (_session params)
  47. "Prepare SESSION according to the header arguments specified in PARAMS."
  48. (let* ((session (cdr (assq :session params)))
  49. (cmd (cdr (assq :cmd params)))
  50. (terminal (cdr (assq :terminal params)))
  51. (screenrc (cdr (assq :screenrc params)))
  52. (process-name (concat "org-babel: terminal (" session ")")))
  53. (apply 'start-process process-name "*Messages*"
  54. terminal `("-T" ,(concat "org-babel: " session) "-e" ,org-babel-screen-location
  55. "-c" ,screenrc "-mS" ,session ,cmd))
  56. ;; XXX: Is there a better way than the following?
  57. (while (not (org-babel-screen-session-socketname session))
  58. ;; wait until screen session is available before returning
  59. )))
  60. ;; helper functions
  61. (defun org-babel-screen-session-execute-string (session body)
  62. "If SESSION exists, send BODY to it."
  63. (let ((socket (org-babel-screen-session-socketname session)))
  64. (when socket
  65. (let ((tmpfile (org-babel-screen-session-write-temp-file session body)))
  66. (apply 'start-process (concat "org-babel: screen (" session ")") "*Messages*"
  67. org-babel-screen-location
  68. `("-S" ,socket "-X" "eval" "msgwait 0"
  69. ,(concat "readreg z " tmpfile)
  70. "paste z"))))))
  71. (defun org-babel-screen-session-socketname (session)
  72. "Check if SESSION exists by parsing output of \"screen -ls\"."
  73. (let* ((screen-ls (shell-command-to-string "screen -ls"))
  74. (sockets (delq
  75. nil
  76. (mapcar
  77. (lambda (x)
  78. (when (string-match (rx (or "(Attached)" "(Detached)")) x)
  79. x))
  80. (split-string screen-ls "\n"))))
  81. (match-socket (car
  82. (delq
  83. nil
  84. (mapcar
  85. (lambda (x)
  86. (and (string-match-p (regexp-quote session) x)
  87. x))
  88. sockets)))))
  89. (when match-socket (car (split-string match-socket)))))
  90. (defun org-babel-screen-session-write-temp-file (_session body)
  91. "Save BODY in a temp file that is named after SESSION."
  92. (let ((tmpfile (org-babel-temp-file "screen-")))
  93. (with-temp-file tmpfile
  94. (insert body)
  95. (insert "\n")
  96. ;; org-babel has superfluous spaces
  97. (goto-char (point-min))
  98. (delete-matching-lines "^ +$"))
  99. tmpfile))
  100. (defun org-babel-screen-test ()
  101. "Test if the default setup works.
  102. The terminal should shortly flicker."
  103. (interactive)
  104. (let* ((random-string (format "%s" (random 99999)))
  105. (tmpfile (org-babel-temp-file "ob-screen-test-"))
  106. (body (concat "echo '" random-string "' > " tmpfile "\nexit\n"))
  107. tmp-string)
  108. (org-babel-execute:screen body org-babel-default-header-args:screen)
  109. ;; XXX: need to find a better way to do the following
  110. (while (not (file-readable-p tmpfile))
  111. ;; do something, otherwise this will be optimized away
  112. (message "org-babel-screen: File not readable yet."))
  113. (setq tmp-string (with-temp-buffer
  114. (insert-file-contents-literally tmpfile)
  115. (buffer-substring (point-min) (point-max))))
  116. (delete-file tmpfile)
  117. (message (concat "org-babel-screen: Setup "
  118. (if (string-match random-string tmp-string)
  119. "WORKS."
  120. "DOESN'T work.")))))
  121. (provide 'ob-screen)
  122. ;;; ob-screen.el ends here