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