ob-screen.el 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ;;; ob-screen.el --- org-babel support for interactive terminal
  2. ;; Copyright (C) 2009-2012 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. (require 'ob-ref)
  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") (:terminal . "xterm"))
  34. "Default arguments to use when running screen source blocks.")
  35. (defun org-babel-execute:screen (body params)
  36. "Send a block of code via screen to a terminal using Babel.
  37. \"default\" session is used when none is specified."
  38. (message "Sending source code block to interactive terminal session...")
  39. (save-window-excursion
  40. (let* ((session (cdr (assoc :session params)))
  41. (socket (org-babel-screen-session-socketname session)))
  42. (unless socket (org-babel-prep-session:screen session params))
  43. (org-babel-screen-session-execute-string
  44. session (org-babel-expand-body:generic body params)))))
  45. (defun org-babel-prep-session:screen (session params)
  46. "Prepare SESSION according to the header arguments specified in PARAMS."
  47. (let* ((session (cdr (assoc :session params)))
  48. (socket (org-babel-screen-session-socketname session))
  49. (cmd (cdr (assoc :cmd params)))
  50. (terminal (cdr (assoc :terminal params)))
  51. (process-name (concat "org-babel: terminal (" session ")")))
  52. (apply 'start-process process-name "*Messages*"
  53. terminal `("-T" ,(concat "org-babel: " session) "-e" ,org-babel-screen-location
  54. "-c" "/dev/null" "-mS" ,(concat "org-babel-session-" session)
  55. ,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. (when (string-match
  87. (concat "org-babel-session-" session) x)
  88. x))
  89. sockets)))))
  90. (when match-socket (car (split-string match-socket)))))
  91. (defun org-babel-screen-session-write-temp-file (session body)
  92. "Save BODY in a temp file that is named after SESSION."
  93. (let ((tmpfile (concat "/tmp/screen.org-babel-session-" session)))
  94. (with-temp-file tmpfile
  95. (insert body)
  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* ((session "org-babel-testing")
  105. (random-string (format "%s" (random 99999)))
  106. (tmpfile "/tmp/org-babel-screen.test")
  107. (body (concat "echo '" random-string "' > " tmpfile "\nexit\n"))
  108. process tmp-string)
  109. (org-babel-execute:screen body org-babel-default-header-args:screen)
  110. ;; XXX: need to find a better way to do the following
  111. (while (not (file-readable-p tmpfile))
  112. ;; do something, otherwise this will be optimized away
  113. (format "org-babel-screen: File not readable yet."))
  114. (setq tmp-string (with-temp-buffer
  115. (insert-file-contents-literally tmpfile)
  116. (buffer-substring (point-min) (point-max))))
  117. (delete-file tmpfile)
  118. (message (concat "org-babel-screen: Setup "
  119. (if (string-match random-string tmp-string)
  120. "WORKS."
  121. "DOESN'T work.")))))
  122. (provide 'ob-screen)
  123. ;;; ob-screen.el ends here