ob-screen.el 6.0 KB

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