ob-screen.el 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ;;; ob-screen.el --- org-babel support for interactive terminal
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation
  3. ;; Author: Benjamin Andresen
  4. ;; Keywords: literate programming, interactive shell
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.01trans
  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 <http://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. (require 'ob-ref)
  30. (defvar org-babel-screen-location "screen"
  31. "The command location for screen.
  32. In case you want to use a different screen than one selected by your $PATH")
  33. (defvar org-babel-default-header-args:screen
  34. '((:results . "silent") (:session . "default") (:cmd . "sh") (:terminal . "xterm"))
  35. "Default arguments to use when running screen source blocks.")
  36. (defun org-babel-expand-body:screen (body params)
  37. "Expand BODY according to PARAMS, return the expanded body." body)
  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* ((processed-params (org-babel-process-params params))
  44. (session (nth 0 processed-params))
  45. (socket (org-babel-screen-session-socketname session)))
  46. (unless socket (org-babel-prep-session:screen session params))
  47. (org-babel-screen-session-execute-string
  48. session (org-babel-expand-body:screen body params)))))
  49. (defun org-babel-prep-session:screen (session params)
  50. "Prepare SESSION according to the header arguments specified in PARAMS."
  51. (let* ((processed-params (org-babel-process-params params))
  52. (session (nth 0 processed-params))
  53. (vars (nth 1 processed-params))
  54. (socket (org-babel-screen-session-socketname session))
  55. (vars (org-babel-ref-variables params))
  56. (cmd (cdr (assoc :cmd params)))
  57. (terminal (cdr (assoc :terminal params)))
  58. (process-name (concat "org-babel: terminal (" session ")")))
  59. (apply 'start-process process-name "*Messages*"
  60. terminal `("-T" ,(concat "org-babel: " session) "-e" ,org-babel-screen-location
  61. "-c" "/dev/null" "-mS" ,(concat "org-babel-session-" session)
  62. ,cmd))
  63. ;; XXX: Is there a better way than the following?
  64. (while (not (org-babel-screen-session-socketname session))
  65. ;; wait until screen session is available before returning
  66. )))
  67. ;; helper functions
  68. (defun org-babel-screen-session-execute-string (session body)
  69. "If SESSION exists, send BODY to it."
  70. (let ((socket (org-babel-screen-session-socketname session)))
  71. (when socket
  72. (let ((tmpfile (org-babel-screen-session-write-temp-file session body)))
  73. (apply 'start-process (concat "org-babel: screen (" session ")") "*Messages*"
  74. org-babel-screen-location
  75. `("-S" ,socket "-X" "eval" "msgwait 0"
  76. ,(concat "readreg z " tmpfile)
  77. "paste z"))))))
  78. (defun org-babel-screen-session-socketname (session)
  79. "Check if SESSION exists by parsing output of \"screen -ls\"."
  80. (let* ((screen-ls (shell-command-to-string "screen -ls"))
  81. (sockets (delq
  82. nil
  83. (mapcar
  84. (lambda (x)
  85. (when (string-match (rx (or "(Attached)" "(Detached)")) x)
  86. x))
  87. (split-string screen-ls "\n"))))
  88. (match-socket (car
  89. (delq
  90. nil
  91. (mapcar
  92. (lambda (x)
  93. (when (string-match
  94. (concat "org-babel-session-" session) x)
  95. x))
  96. sockets)))))
  97. (when match-socket (car (split-string match-socket)))))
  98. (defun org-babel-screen-session-write-temp-file (session body)
  99. "Save BODY in a temp file that is named after SESSION."
  100. (let ((tmpfile (concat "/tmp/screen.org-babel-session-" session)))
  101. (with-temp-file tmpfile
  102. (insert body)
  103. ;; org-babel has superflous spaces
  104. (goto-char (point-min))
  105. (delete-matching-lines "^ +$"))
  106. tmpfile))
  107. (defun org-babel-screen-test ()
  108. "Test if the default setup works.
  109. The terminal should shortly flicker."
  110. (interactive)
  111. (let* ((session "org-babel-testing")
  112. (random-string (format "%s" (random 99999)))
  113. (tmpfile "/tmp/org-babel-screen.test")
  114. (body (concat "echo '" random-string "' > " tmpfile "\nexit\n"))
  115. process tmp-string)
  116. (org-babel-execute:screen body org-babel-default-header-args:screen)
  117. ;; XXX: need to find a better way to do the following
  118. (while (not (file-readable-p tmpfile))
  119. ;; do something, otherwise this will be optimized away
  120. (format "org-babel-screen: File not readable yet."))
  121. (setq tmp-string (with-temp-buffer
  122. (insert-file-contents-literally tmpfile)
  123. (buffer-substring (point-min) (point-max))))
  124. (delete-file tmpfile)
  125. (message (concat "org-babel-screen: Setup "
  126. (if (string-match random-string tmp-string)
  127. "WORKS."
  128. "DOESN'T work.")))))
  129. (provide 'ob-screen)
  130. ;; arch-tag: 908e5afe-89a0-4f27-b982-23f1f2e3bac9
  131. ;;; ob-screen.el ends here