ob-screen.el 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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: 0.01
  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. (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-expand-body:screen (body params &optional processed-params)
  36. "Expand BODY according to PARAMS, return the expanded body." body)
  37. (defun org-babel-execute:screen (body params)
  38. "Send a block of code via screen to a terminal using org-babel.
  39. \"default\" session is be used when none is specified."
  40. (message "Sending source code block to interactive terminal session...")
  41. (save-window-excursion
  42. (let* ((processed-params (org-babel-process-params params))
  43. (session (first processed-params))
  44. (socket (org-babel-screen-session-socketname session)))
  45. (unless socket (org-babel-prep-session:screen session params))
  46. (org-babel-screen-session-execute-string
  47. session (org-babel-expand-body:screen body params)))))
  48. (defun org-babel-prep-session:screen (session params)
  49. "Prepare SESSION according to the header arguments specified in PARAMS."
  50. (let* ((processed-params (org-babel-process-params params))
  51. (session (first processed-params))
  52. (vars (nth 1 processed-params))
  53. (socket (org-babel-screen-session-socketname session))
  54. (vars (org-babel-ref-variables params))
  55. (cmd (cdr (assoc :cmd params)))
  56. (terminal (cdr (assoc :terminal params)))
  57. (process-name (concat "org-babel: terminal (" session ")")))
  58. (apply 'start-process process-name "*Messages*"
  59. terminal `("-T" ,(concat "org-babel: " session) "-e" ,org-babel-screen-location
  60. "-c" "/dev/null" "-mS" ,(concat "org-babel-session-" session)
  61. ,cmd))
  62. ;; XXX: Is there a better way than the following?
  63. (while (not (org-babel-screen-session-socketname session))
  64. ;; wait until screen session is available before returning
  65. )))
  66. ;; helper functions
  67. (defun org-babel-screen-session-execute-string (session body)
  68. "If SESSION exist, send BODY to it."
  69. (let ((socket (org-babel-screen-session-socketname session)))
  70. (when socket
  71. (let ((tmpfile (org-babel-screen-session-write-temp-file session body)))
  72. (apply 'start-process (concat "org-babel: screen (" session ")") "*Messages*"
  73. org-babel-screen-location
  74. `("-S" ,socket "-X" "eval" "msgwait 0"
  75. ,(concat "readreg z " tmpfile)
  76. "paste z"))))))
  77. (defun org-babel-screen-session-socketname (session)
  78. "Check if SESSION exist by parsing output of \"screen -ls\"."
  79. (let* ((screen-ls (shell-command-to-string "screen -ls"))
  80. (sockets (remove-if-not
  81. '(lambda (x)
  82. (string-match (rx (or "(Attached)" "(Detached)")) x))
  83. (split-string screen-ls "\n")))
  84. (match-socket (find-if
  85. '(lambda (x)
  86. (string-match (concat "org-babel-session-" session) x))
  87. sockets)))
  88. (when match-socket (car (split-string match-socket)))))
  89. (defun org-babel-screen-session-write-temp-file (session body)
  90. "Save BODY in a temp file that is named after SESSION."
  91. (let ((tmpfile (concat "/tmp/screen.org-babel-session-" session)))
  92. (with-temp-file tmpfile
  93. (insert body)
  94. ;; org-babel has superflous spaces
  95. (goto-char (point-min))
  96. (delete-matching-lines "^ +$"))
  97. tmpfile))
  98. (defun org-babel-screen-test ()
  99. "Test if the default setup works. The terminal should shortly
  100. flicker."
  101. (interactive)
  102. (let* ((session "org-babel-testing")
  103. (random-string (format "%s" (random 99999)))
  104. (tmpfile "/tmp/org-babel-screen.test")
  105. (body (concat "echo '" random-string "' > " tmpfile "\nexit\n"))
  106. process tmp-string)
  107. (org-babel-execute:screen body org-babel-default-header-args:screen)
  108. ;; XXX: need to find a better way to do the following
  109. (while (not (file-readable-p tmpfile))
  110. ;; do something, otherwise this will be optimized away
  111. (format "org-babel-screen: File not readable yet."))
  112. (setq tmp-string (with-temp-buffer
  113. (insert-file-contents-literally tmpfile)
  114. (buffer-substring (point-min) (point-max))))
  115. (delete-file tmpfile)
  116. (message (concat "org-babel-screen: Setup "
  117. (if (string-match random-string tmp-string)
  118. "WORKS."
  119. "DOESN'T work.")))))
  120. (provide 'ob-screen)
  121. ;; arch-tag: 908e5afe-89a0-4f27-b982-23f1f2e3bac9
  122. ;;; ob-screen.el ends here