org-babel-screen.el 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ;;; org-babel-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 'org-babel)
  33. (org-babel-add-interpreter "screen")
  34. (add-to-list 'org-src-lang-modes '("screen" . sh))
  35. (defvar org-babel-screen-location "screen"
  36. "The command location for screen.
  37. In case you want to use a different screen than one selected by your $PATH")
  38. (defvar org-babel-default-header-args:screen
  39. '((:results . "silent") (:session . "default") (:cmd . "sh") (:terminal . "xterm"))
  40. "Default arguments to use when running screen source blocks.")
  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 session body))))
  51. (defun org-babel-prep-session:screen (session params)
  52. "Prepare SESSION according to the header arguments specified in PARAMS."
  53. (let* ((processed-params (org-babel-process-params params))
  54. (session (first processed-params))
  55. (vars (second processed-params))
  56. (socket (org-babel-screen-session-socketname session))
  57. (vars (org-babel-ref-variables params))
  58. (cmd (cdr (assoc :cmd params)))
  59. (terminal (cdr (assoc :terminal params)))
  60. (process-name (concat "org-babel: terminal (" session ")")))
  61. (apply 'start-process process-name "*Messages*"
  62. terminal `("-T" ,(concat "org-babel: " session) "-e" ,org-babel-screen-location
  63. "-c" "/dev/null" "-mS" ,(concat "org-babel-session-" session)
  64. ,cmd))
  65. ;; XXX: Is there a better way than the following?
  66. (while (not (org-babel-screen-session-socketname session))
  67. ;; wait until screen session is available before returning
  68. )))
  69. ;; helper functions
  70. (defun org-babel-screen-session-execute-string (session body)
  71. "If SESSION exist, send BODY to it."
  72. (let ((socket (org-babel-screen-session-socketname session)))
  73. (when socket
  74. (let ((tmpfile (org-babel-screen-session-write-temp-file session body)))
  75. (apply 'start-process (concat "org-babel: screen (" session ")") "*Messages*"
  76. org-babel-screen-location
  77. `("-S" ,socket "-X" "eval" "msgwait 0"
  78. ,(concat "readreg z " tmpfile)
  79. "paste z"))))))
  80. (defun org-babel-screen-session-socketname (session)
  81. "Check if SESSION exist by parsing output of \"screen -ls\"."
  82. (let* ((screen-ls (shell-command-to-string "screen -ls"))
  83. (sockets (remove-if-not
  84. '(lambda (x)
  85. (string-match (rx (or "(Attached)" "(Detached)")) x))
  86. (split-string screen-ls "\n")))
  87. (match-socket (find-if
  88. '(lambda (x)
  89. (string-match (concat "org-babel-session-" session) 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. "Command that tests 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 'org-babel-screen)
  124. ;;; org-babel-screen.el ends here