1 ;;; ob-screen.el --- org-babel support for interactive terminal
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 ;; Author: Benjamin Andresen
6 ;; Keywords: literate programming, interactive shell
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Org-Babel support for interactive terminals. Mostly shell scripts.
27 ;; Heavily inspired by 'eev' from Eduardo Ochs
29 ;; Adding :cmd and :terminal as header arguments
30 ;; :terminal must support the -T (title) and -e (command) parameter
32 ;; You can test the default setup. (xterm + sh) with
33 ;; M-x org-babel-screen-test RET
39 (defvar org-babel-screen-location
"screen"
40 "The command location for screen.
41 In case you want to use a different screen than one selected by your $PATH")
43 (defvar org-babel-default-header-args
:screen
44 '((:results .
"silent") (:session .
"default") (:cmd .
"sh") (:terminal .
"xterm"))
45 "Default arguments to use when running screen source blocks.")
47 (defun org-babel-execute:screen
(body params
)
48 "Send a block of code via screen to a terminal using Babel.
49 \"default\" session is used when none is specified."
50 (message "Sending source code block to interactive terminal session...")
51 (save-window-excursion
52 (let* ((session (cdr (assoc :session params
)))
53 (socket (org-babel-screen-session-socketname session
)))
54 (unless socket
(org-babel-prep-session:screen session params
))
55 (org-babel-screen-session-execute-string
56 session
(org-babel-expand-body:generic body params
)))))
58 (defun org-babel-prep-session:screen
(session params
)
59 "Prepare SESSION according to the header arguments specified in PARAMS."
60 (let* ((session (cdr (assoc :session params
)))
61 (socket (org-babel-screen-session-socketname session
))
62 (cmd (cdr (assoc :cmd params
)))
63 (terminal (cdr (assoc :terminal params
)))
64 (process-name (concat "org-babel: terminal (" session
")")))
65 (apply 'start-process process-name
"*Messages*"
66 terminal
`("-T" ,(concat "org-babel: " session
) "-e" ,org-babel-screen-location
67 "-c" "/dev/null" "-mS" ,(concat "org-babel-session-" session
)
69 ;; XXX: Is there a better way than the following?
70 (while (not (org-babel-screen-session-socketname session
))
71 ;; wait until screen session is available before returning
76 (defun org-babel-screen-session-execute-string (session body
)
77 "If SESSION exists, send BODY to it."
78 (let ((socket (org-babel-screen-session-socketname session
)))
80 (let ((tmpfile (org-babel-screen-session-write-temp-file session body
)))
81 (apply 'start-process
(concat "org-babel: screen (" session
")") "*Messages*"
82 org-babel-screen-location
83 `("-S" ,socket
"-X" "eval" "msgwait 0"
84 ,(concat "readreg z " tmpfile
)
87 (defun org-babel-screen-session-socketname (session)
88 "Check if SESSION exists by parsing output of \"screen -ls\"."
89 (let* ((screen-ls (shell-command-to-string "screen -ls"))
94 (when (string-match (rx (or "(Attached)" "(Detached)")) x
)
96 (split-string screen-ls
"\n"))))
103 (concat "org-babel-session-" session
) x
)
106 (when match-socket
(car (split-string match-socket
)))))
108 (defun org-babel-screen-session-write-temp-file (session body
)
109 "Save BODY in a temp file that is named after SESSION."
110 (let ((tmpfile (concat "/tmp/screen.org-babel-session-" session
)))
111 (with-temp-file tmpfile
114 ;; org-babel has superfluous spaces
115 (goto-char (point-min))
116 (delete-matching-lines "^ +$"))
119 (defun org-babel-screen-test ()
120 "Test if the default setup works.
121 The terminal should shortly flicker."
123 (let* ((session "org-babel-testing")
124 (random-string (format "%s" (random 99999)))
125 (tmpfile "/tmp/org-babel-screen.test")
126 (body (concat "echo '" random-string
"' > " tmpfile
"\nexit\n"))
128 (org-babel-execute:screen body org-babel-default-header-args
:screen
)
129 ;; XXX: need to find a better way to do the following
130 (while (not (file-readable-p tmpfile
))
131 ;; do something, otherwise this will be optimized away
132 (format "org-babel-screen: File not readable yet."))
133 (setq tmp-string
(with-temp-buffer
134 (insert-file-contents-literally tmpfile
)
135 (buffer-substring (point-min) (point-max))))
136 (delete-file tmpfile
)
137 (message (concat "org-babel-screen: Setup "
138 (if (string-match random-string tmp-string
)
146 ;;; ob-screen.el ends here