ob-screen.el 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.3
  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-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 (assoc :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 (assoc :session params)))
  49. (socket (org-babel-screen-session-socketname session))
  50. (cmd (cdr (assoc :cmd params)))
  51. (terminal (cdr (assoc :terminal 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" "/dev/null" "-mS" ,(concat "org-babel-session-" session)
  56. ,cmd))
  57. ;; XXX: Is there a better way than the following?
  58. (while (not (org-babel-screen-session-socketname session))
  59. ;; wait until screen session is available before returning
  60. )))
  61. ;; helper functions
  62. (defun org-babel-screen-session-execute-string (session body)
  63. "If SESSION exists, send BODY to it."
  64. (let ((socket (org-babel-screen-session-socketname session)))
  65. (when socket
  66. (let ((tmpfile (org-babel-screen-session-write-temp-file session body)))
  67. (apply 'start-process (concat "org-babel: screen (" session ")") "*Messages*"
  68. org-babel-screen-location
  69. `("-S" ,socket "-X" "eval" "msgwait 0"
  70. ,(concat "readreg z " tmpfile)
  71. "paste z"))))))
  72. (defun org-babel-screen-session-socketname (session)
  73. "Check if SESSION exists by parsing output of \"screen -ls\"."
  74. (let* ((screen-ls (shell-command-to-string "screen -ls"))
  75. (sockets (delq
  76. nil
  77. (mapcar
  78. (lambda (x)
  79. (when (string-match (rx (or "(Attached)" "(Detached)")) x)
  80. x))
  81. (split-string screen-ls "\n"))))
  82. (match-socket (car
  83. (delq
  84. nil
  85. (mapcar
  86. (lambda (x)
  87. (when (string-match
  88. (concat "org-babel-session-" session) x)
  89. x))
  90. sockets)))))
  91. (when match-socket (car (split-string match-socket)))))
  92. (defun org-babel-screen-session-write-temp-file (session body)
  93. "Save BODY in a temp file that is named after SESSION."
  94. (let ((tmpfile (concat "/tmp/screen.org-babel-session-" session)))
  95. (with-temp-file tmpfile
  96. (insert body)
  97. ;; org-babel has superflous spaces
  98. (goto-char (point-min))
  99. (delete-matching-lines "^ +$"))
  100. tmpfile))
  101. (defun org-babel-screen-test ()
  102. "Test if the default setup works.
  103. The terminal should shortly flicker."
  104. (interactive)
  105. (let* ((session "org-babel-testing")
  106. (random-string (format "%s" (random 99999)))
  107. (tmpfile "/tmp/org-babel-screen.test")
  108. (body (concat "echo '" random-string "' > " tmpfile "\nexit\n"))
  109. process tmp-string)
  110. (org-babel-execute:screen body org-babel-default-header-args:screen)
  111. ;; XXX: need to find a better way to do the following
  112. (while (not (file-readable-p tmpfile))
  113. ;; do something, otherwise this will be optimized away
  114. (format "org-babel-screen: File not readable yet."))
  115. (setq tmp-string (with-temp-buffer
  116. (insert-file-contents-literally tmpfile)
  117. (buffer-substring (point-min) (point-max))))
  118. (delete-file tmpfile)
  119. (message (concat "org-babel-screen: Setup "
  120. (if (string-match random-string tmp-string)
  121. "WORKS."
  122. "DOESN'T work.")))))
  123. (provide 'ob-screen)
  124. ;; arch-tag: 908e5afe-89a0-4f27-b982-23f1f2e3bac9
  125. ;;; ob-screen.el ends here