Fix indentation problem
[org-mode.git] / lisp / ob-screen.el
blob7f4af795499be59021b1df8654208f8454bdbc56
1 ;;; ob-screen.el --- org-babel support for interactive terminal
3 ;; Copyright (C) 2009, 2010 Free Software Foundation
5 ;; Author: Benjamin Andresen
6 ;; Keywords: literate programming, interactive shell
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.4
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
27 ;; Org-Babel support for interactive terminals. Mostly shell scripts.
28 ;; Heavily inspired by 'eev' from Eduardo Ochs
30 ;; Adding :cmd and :terminal as header arguments
31 ;; :terminal must support the -T (title) and -e (command) parameter
33 ;; You can test the default setup. (xterm + sh) with
34 ;; M-x org-babel-screen-test RET
36 ;;; Code:
37 (require 'ob)
38 (require 'ob-ref)
40 (defvar org-babel-screen-location "screen"
41 "The command location for screen.
42 In case you want to use a different screen than one selected by your $PATH")
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.")
48 (defun org-babel-execute:screen (body params)
49 "Send a block of code via screen to a terminal using Babel.
50 \"default\" session is used when none is specified."
51 (message "Sending source code block to interactive terminal session...")
52 (save-window-excursion
53 (let* ((session (cdr (assoc :session params)))
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
57 session (org-babel-expand-body:generic body params)))))
59 (defun org-babel-prep-session:screen (session params)
60 "Prepare SESSION according to the header arguments specified in PARAMS."
61 (let* ((session (cdr (assoc :session params)))
62 (socket (org-babel-screen-session-socketname session))
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 )))
75 ;; helper functions
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"))))))
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
101 (mapcar
102 (lambda (x)
103 (when (string-match
104 (concat "org-babel-session-" session) x)
106 sockets)))))
107 (when match-socket (car (split-string match-socket)))))
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)
115 ;; org-babel has superflous spaces
116 (goto-char (point-min))
117 (delete-matching-lines "^ +$"))
118 tmpfile))
120 (defun org-babel-screen-test ()
121 "Test if the default setup works.
122 The 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.")))))
143 (provide 'ob-screen)
145 ;; arch-tag: 908e5afe-89a0-4f27-b982-23f1f2e3bac9
147 ;;; ob-screen.el ends here