ob-core: All Noweb parameters are read at point of definition
[org-mode.git] / lisp / ob-screen.el
blobfbf167e0e41e0bfc1c196ffab2b3ee2cd709bc55
1 ;;; ob-screen.el --- Babel Support for Interactive Terminal -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2017 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 <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
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
35 ;;; Code:
36 (require 'ob)
38 (defvar org-babel-screen-location "screen"
39 "The command location for screen.
40 In case you want to use a different screen than one selected by your $PATH")
42 (defvar org-babel-default-header-args:screen
43 '((:results . "silent") (:session . "default") (:cmd . "sh") (:terminal . "xterm"))
44 "Default arguments to use when running screen source blocks.")
46 (defun org-babel-execute:screen (body params)
47 "Send a block of code via screen to a terminal using Babel.
48 \"default\" session is used when none is specified."
49 (message "Sending source code block to interactive terminal session...")
50 (save-window-excursion
51 (let* ((session (cdr (assq :session params)))
52 (socket (org-babel-screen-session-socketname session)))
53 (unless socket (org-babel-prep-session:screen session params))
54 (org-babel-screen-session-execute-string
55 session (org-babel-expand-body:generic body params)))))
57 (defun org-babel-prep-session:screen (_session params)
58 "Prepare SESSION according to the header arguments specified in PARAMS."
59 (let* ((session (cdr (assq :session params)))
60 (cmd (cdr (assq :cmd params)))
61 (terminal (cdr (assq :terminal params)))
62 (process-name (concat "org-babel: terminal (" session ")")))
63 (apply 'start-process process-name "*Messages*"
64 terminal `("-T" ,(concat "org-babel: " session) "-e" ,org-babel-screen-location
65 "-c" "/dev/null" "-mS" ,(concat "org-babel-session-" session)
66 ,cmd))
67 ;; XXX: Is there a better way than the following?
68 (while (not (org-babel-screen-session-socketname session))
69 ;; wait until screen session is available before returning
70 )))
72 ;; helper functions
74 (defun org-babel-screen-session-execute-string (session body)
75 "If SESSION exists, send BODY to it."
76 (let ((socket (org-babel-screen-session-socketname session)))
77 (when socket
78 (let ((tmpfile (org-babel-screen-session-write-temp-file session body)))
79 (apply 'start-process (concat "org-babel: screen (" session ")") "*Messages*"
80 org-babel-screen-location
81 `("-S" ,socket "-X" "eval" "msgwait 0"
82 ,(concat "readreg z " tmpfile)
83 "paste z"))))))
85 (defun org-babel-screen-session-socketname (session)
86 "Check if SESSION exists by parsing output of \"screen -ls\"."
87 (let* ((screen-ls (shell-command-to-string "screen -ls"))
88 (sockets (delq
89 nil
90 (mapcar
91 (lambda (x)
92 (when (string-match (rx (or "(Attached)" "(Detached)")) x)
93 x))
94 (split-string screen-ls "\n"))))
95 (match-socket (car
96 (delq
97 nil
98 (mapcar
99 (lambda (x)
100 (when (string-match
101 (concat "org-babel-session-" session) x)
103 sockets)))))
104 (when match-socket (car (split-string match-socket)))))
106 (defun org-babel-screen-session-write-temp-file (_session body)
107 "Save BODY in a temp file that is named after SESSION."
108 (let ((tmpfile (org-babel-temp-file "screen-")))
109 (with-temp-file tmpfile
110 (insert body)
112 ;; org-babel has superfluous spaces
113 (goto-char (point-min))
114 (delete-matching-lines "^ +$"))
115 tmpfile))
117 (defun org-babel-screen-test ()
118 "Test if the default setup works.
119 The terminal should shortly flicker."
120 (interactive)
121 (let* ((random-string (format "%s" (random 99999)))
122 (tmpfile (org-babel-temp-file "ob-screen-test-"))
123 (body (concat "echo '" random-string "' > " tmpfile "\nexit\n"))
124 tmp-string)
125 (org-babel-execute:screen body org-babel-default-header-args:screen)
126 ;; XXX: need to find a better way to do the following
127 (while (not (file-readable-p tmpfile))
128 ;; do something, otherwise this will be optimized away
129 (format "org-babel-screen: File not readable yet."))
130 (setq tmp-string (with-temp-buffer
131 (insert-file-contents-literally tmpfile)
132 (buffer-substring (point-min) (point-max))))
133 (delete-file tmpfile)
134 (message (concat "org-babel-screen: Setup "
135 (if (string-match random-string tmp-string)
136 "WORKS."
137 "DOESN'T work.")))))
139 (provide 'ob-screen)
143 ;;; ob-screen.el ends here