ob-screen.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ;;; ob-screen.el --- org-babel support for interactive terminal
  2. ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
  3. ;; Author: Benjamin Andresen
  4. ;; Keywords: literate programming, interactive shell
  5. ;; Homepage: http://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 <http://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 (assoc :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 (assoc :session params)))
  47. (socket (org-babel-screen-session-socketname session))
  48. (cmd (cdr (assoc :cmd params)))
  49. (terminal (cdr (assoc :terminal params)))
  50. (process-name (concat "org-babel: terminal (" session ")")))
  51. (apply 'start-process process-name "*Messages*"
  52. terminal `("-T" ,(concat "org-babel: " session) "-e" ,org-babel-screen-location
  53. "-c" "/dev/null" "-mS" ,(concat "org-babel-session-" session)
  54. ,cmd))
  55. ;; XXX: Is there a better way than the following?
  56. (while (not (org-babel-screen-session-socketname session))
  57. ;; wait until screen session is available before returning
  58. )))
  59. ;; helper functions
  60. (defun org-babel-screen-session-execute-string (session body)
  61. "If SESSION exists, send BODY to it."
  62. (let ((socket (org-babel-screen-session-socketname session)))
  63. (when socket
  64. (let ((tmpfile (org-babel-screen-session-write-temp-file session body)))
  65. (apply 'start-process (concat "org-babel: screen (" session ")") "*Messages*"
  66. org-babel-screen-location
  67. `("-S" ,socket "-X" "eval" "msgwait 0"
  68. ,(concat "readreg z " tmpfile)
  69. "paste z"))))))
  70. (defun org-babel-screen-session-socketname (session)
  71. "Check if SESSION exists by parsing output of \"screen -ls\"."
  72. (let* ((screen-ls (shell-command-to-string "screen -ls"))
  73. (sockets (delq
  74. nil
  75. (mapcar
  76. (lambda (x)
  77. (when (string-match (rx (or "(Attached)" "(Detached)")) x)
  78. x))
  79. (split-string screen-ls "\n"))))
  80. (match-socket (car
  81. (delq
  82. nil
  83. (mapcar
  84. (lambda (x)
  85. (when (string-match
  86. (concat "org-babel-session-" 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. ;; org-babel has superfluous spaces
  96. (goto-char (point-min))
  97. (delete-matching-lines "^ +$"))
  98. tmpfile))
  99. (defun org-babel-screen-test ()
  100. "Test if the default setup works.
  101. The terminal should shortly flicker."
  102. (interactive)
  103. (let* ((session "org-babel-testing")
  104. (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. process 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. (format "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