ob-screen.el 5.4 KB

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