babel: remove all language file reference to define language variables
[org-mode.git] / lisp / babel / langs / ob-screen.el
blob8c690e89d54b0580360ed97201419e25684ac8d4
1 ;;; ob-screen.el --- org-babel support for interactive terminal
3 ;; Copyright (C) 2009 Benjamin Andresen
5 ;; Author: Benjamin Andresen
6 ;; Keywords: literate programming, interactive shell
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;;; License:
12 ;; This program 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, or (at your option)
15 ;; any later version.
17 ;; This program 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; Org-Babel support for interactive terminals. Mostly shell scripts.
30 ;; Heavily inspired by 'eev' from Eduardo Ochs
32 ;; Adding :cmd and :terminal as header arguments
33 ;; :terminal must support the -T (title) and -e (command) parameter
35 ;; You can test the default setup. (xterm + sh) with
36 ;; M-x org-babel-screen-test RET
38 ;;; Code:
39 (require 'ob)
41 (defvar org-babel-screen-location "screen"
42 "The command location for screen.
43 In case you want to use a different screen than one selected by your $PATH")
45 (defvar org-babel-default-header-args:screen
46 '((:results . "silent") (:session . "default") (:cmd . "sh") (:terminal . "xterm"))
47 "Default arguments to use when running screen source blocks.")
49 (defun org-babel-expand-body:screen (body params &optional processed-params)
50 "Expand BODY according to PARAMS, return the expanded body." body)
52 (defun org-babel-execute:screen (body params)
53 "Send a block of code via screen to a terminal using org-babel.
54 \"default\" session is be used when none is specified."
55 (message "Sending source code block to interactive terminal session...")
56 (save-window-excursion
57 (let* ((processed-params (org-babel-process-params params))
58 (session (first processed-params))
59 (socket (org-babel-screen-session-socketname session)))
60 (unless socket (org-babel-prep-session:screen session params))
61 (org-babel-screen-session-execute-string
62 session (org-babel-expand-body:screen body params)))))
64 (defun org-babel-prep-session:screen (session params)
65 "Prepare SESSION according to the header arguments specified in PARAMS."
66 (let* ((processed-params (org-babel-process-params params))
67 (session (first processed-params))
68 (vars (second processed-params))
69 (socket (org-babel-screen-session-socketname session))
70 (vars (org-babel-ref-variables params))
71 (cmd (cdr (assoc :cmd params)))
72 (terminal (cdr (assoc :terminal params)))
73 (process-name (concat "org-babel: terminal (" session ")")))
74 (apply 'start-process process-name "*Messages*"
75 terminal `("-T" ,(concat "org-babel: " session) "-e" ,org-babel-screen-location
76 "-c" "/dev/null" "-mS" ,(concat "org-babel-session-" session)
77 ,cmd))
78 ;; XXX: Is there a better way than the following?
79 (while (not (org-babel-screen-session-socketname session))
80 ;; wait until screen session is available before returning
81 )))
83 ;; helper functions
85 (defun org-babel-screen-session-execute-string (session body)
86 "If SESSION exist, send BODY to it."
87 (let ((socket (org-babel-screen-session-socketname session)))
88 (when socket
89 (let ((tmpfile (org-babel-screen-session-write-temp-file session body)))
90 (apply 'start-process (concat "org-babel: screen (" session ")") "*Messages*"
91 org-babel-screen-location
92 `("-S" ,socket "-X" "eval" "msgwait 0"
93 ,(concat "readreg z " tmpfile)
94 "paste z"))))))
96 (defun org-babel-screen-session-socketname (session)
97 "Check if SESSION exist by parsing output of \"screen -ls\"."
98 (let* ((screen-ls (shell-command-to-string "screen -ls"))
99 (sockets (remove-if-not
100 '(lambda (x)
101 (string-match (rx (or "(Attached)" "(Detached)")) x))
102 (split-string screen-ls "\n")))
103 (match-socket (find-if
104 '(lambda (x)
105 (string-match (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. The terminal should shortly
122 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)
144 ;;; ob-screen.el ends here