Sync Org 7.9.2 from the commit tagged "release_7.9.2" in Org's Git repo.
[bpt/emacs.git] / lisp / org / ob-screen.el
CommitLineData
86fbb8ca
CD
1;;; ob-screen.el --- org-babel support for interactive terminal
2
c7e9ed79 3;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
86fbb8ca
CD
4
5;; Author: Benjamin Andresen
6;; Keywords: literate programming, interactive shell
7;; Homepage: http://orgmode.org
86fbb8ca
CD
8
9;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24;;; Commentary:
25
8223b1d2 26;; Org-Babel support for interactive terminals. Mostly shell scripts.
86fbb8ca
CD
27;; Heavily inspired by 'eev' from Eduardo Ochs
28;;
29;; Adding :cmd and :terminal as header arguments
30;; :terminal must support the -T (title) and -e (command) parameter
31;;
32;; You can test the default setup. (xterm + sh) with
33;; M-x org-babel-screen-test RET
34
35;;; Code:
36(require 'ob)
37(require 'ob-ref)
38
39(defvar org-babel-screen-location "screen"
22bcf204 40 "The command location for screen.
86fbb8ca
CD
41In case you want to use a different screen than one selected by your $PATH")
42
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.")
46
86fbb8ca
CD
47(defun org-babel-execute:screen (body params)
48 "Send a block of code via screen to a terminal using Babel.
afe98dfa 49\"default\" session is used when none is specified."
86fbb8ca
CD
50 (message "Sending source code block to interactive terminal session...")
51 (save-window-excursion
afe98dfa 52 (let* ((session (cdr (assoc :session params)))
86fbb8ca
CD
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
afe98dfa 56 session (org-babel-expand-body:generic body params)))))
86fbb8ca
CD
57
58(defun org-babel-prep-session:screen (session params)
59 "Prepare SESSION according to the header arguments specified in PARAMS."
afe98dfa 60 (let* ((session (cdr (assoc :session params)))
86fbb8ca 61 (socket (org-babel-screen-session-socketname session))
86fbb8ca
CD
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
8223b1d2
BG
67 "-c" "/dev/null" "-mS" ,(concat "org-babel-session-" session)
68 ,cmd))
86fbb8ca
CD
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
72 )))
73
74;; helper functions
75
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)))
79 (when socket
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"
8223b1d2
BG
84 ,(concat "readreg z " tmpfile)
85 "paste z"))))))
86fbb8ca
CD
86
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"))
90 (sockets (delq
91 nil
92 (mapcar
93 (lambda (x)
94 (when (string-match (rx (or "(Attached)" "(Detached)")) x)
95 x))
96 (split-string screen-ls "\n"))))
97 (match-socket (car
98 (delq
99 nil
100 (mapcar
101 (lambda (x)
102 (when (string-match
103 (concat "org-babel-session-" session) x)
104 x))
105 sockets)))))
106 (when match-socket (car (split-string match-socket)))))
107
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
112 (insert body)
113
22bcf204 114 ;; org-babel has superfluous spaces
86fbb8ca
CD
115 (goto-char (point-min))
116 (delete-matching-lines "^ +$"))
117 tmpfile))
118
119(defun org-babel-screen-test ()
120 "Test if the default setup works.
121The terminal should shortly flicker."
122 (interactive)
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"))
127 process tmp-string)
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)
139 "WORKS."
8223b1d2 140 "DOESN'T work.")))))
86fbb8ca
CD
141
142(provide 'ob-screen)
143
5b409b39 144
86fbb8ca
CD
145
146;;; ob-screen.el ends here