forked from mirrors/org-mode
* lisp/ob-C.el (org-babel-prep-session:C): (org-babel-load-session:C): * lisp/ob-J.el: (org-babel-expand-body:J): (org-babel-execute:J): * lisp/ob-R.el: (org-babel-expand-body:R): * lisp/ob-abc.el: (org-babel-execute:abc): (org-babel-prep-session:abc): * lisp/ob-asymptote.el: (org-babel-execute:asymptote): (org-babel-prep-session:asymptote): * lisp/ob-awk.el: (org-babel-expand-body:awk): * lisp/ob-calc.el: (org-babel-expand-body:calc): * lisp/ob-clojure.el: * lisp/ob-comint.el: (org-babel-comint-in-buffer): (org-babel-comint-with-output): (org-babel-comint-eval-invisibly-and-wait-for-file): * lisp/ob-coq.el: * lisp/ob-css.el: (org-babel-execute:css): (org-babel-prep-session:css): * lisp/ob-ditaa.el: (org-babel-execute:ditaa): (org-babel-prep-session:ditaa): * lisp/ob-dot.el: (org-babel-execute:dot): (org-babel-prep-session:dot): * lisp/ob-ebnf.el: * lisp/ob-emacs-lisp.el: * lisp/ob-eval.el: * lisp/ob-forth.el: * lisp/ob-fortran.el: (org-babel-execute:fortran): (org-babel-prep-session:fortran): (org-babel-load-session:fortran): * lisp/ob-gnuplot.el: (org-babel-expand-body:gnuplot): (org-babel-prep-session:gnuplot): (org-babel-gnuplot-initiate-session): * lisp/ob-groovy.el: (org-babel-prep-session:groovy): (org-babel-groovy-initiate-session): * lisp/ob-haskell.el: (org-babel-haskell-initiate-session): * lisp/ob-io.el: (org-babel-prep-session:io): (org-babel-io-initiate-session): * lisp/ob-java.el: (org-babel-execute:java): * lisp/ob-js.el: * lisp/ob-keys.el: * lisp/ob-latex.el: (org-babel-prep-session:latex): * lisp/ob-ledger.el: (org-babel-execute:ledger): (org-babel-prep-session:ledger): * lisp/ob-lilypond.el: (org-babel-lilypond-commands): (org-babel-lilypond-process-basic): (org-babel-prep-session:lilypond): (org-babel-lilypond-parse-line-num): * lisp/ob-lisp.el: * lisp/ob-makefile.el: (org-babel-execute:makefile): (org-babel-prep-session:makefile): * lisp/ob-matlab.el: * lisp/ob-maxima.el: (org-babel-prep-session:maxima): * lisp/ob-mscgen.el: (org-babel-prep-session:mscgen): * lisp/ob-ocaml.el: (org-babel-execute:ocaml): (org-babel-prep-session:ocaml): * lisp/ob-octave.el: (org-babel-execute:octave): (org-babel-octave-initiate-session): * lisp/ob-org.el: (org-babel-prep-session:org): * lisp/ob-perl.el: (org-babel-prep-session:perl): (org-babel-perl--var-to-perl): (org-babel-perl-initiate-session): * lisp/ob-picolisp.el: (org-babel-expand-body:picolisp): (org-babel-execute:picolisp): * lisp/ob-plantuml.el: (org-babel-execute:plantuml): (org-babel-prep-session:plantuml): * lisp/ob-processing.el: (org-babel-prep-session:processing): * lisp/ob-python.el: (org-babel-python-initiate-session): * lisp/ob-ref.el: (org-babel-ref-resolve): * lisp/ob-ruby.el: (org-babel-ruby-initiate-session): * lisp/ob-sass.el: (org-babel-execute:sass): (org-babel-prep-session:sass): * lisp/ob-scala.el: (org-babel-execute:scala): (org-babel-prep-session:scala): (org-babel-scala-initiate-session): * lisp/ob-scheme.el: * lisp/ob-screen.el: (org-babel-prep-session:screen): (org-babel-screen-session-write-temp-file): (org-babel-screen-test): * lisp/ob.el: * lisp/org-colview.el: (org-columns-todo): (org-columns-set-tags-or-toggle): (org-columns-new): (org-columns-uncompile-format): (org-agenda-colview-summarize): * lisp/org-footnote.el: (electric-indent-mode): * lisp/org-indent.el: (org-indent-refresh-maybe): * lisp/org-list.el: * lisp/org-macro.el: (org-macro--collect-macros): * lisp/org-src.el: * lisp/org-table.el: (sort-fold-case): (org-table-create): (org-table-field-info): (org-table-transpose-table-at-point): (org-table-remove-rectangle-highlight): (orgtbl-create-or-convert-from-region): (org-define-lookup-function): * lisp/ox-ascii.el: (org-ascii-format-drawer-function): (org-ascii--has-caption-p): (org-ascii-bold): (org-ascii-center-block): (org-ascii-clock): (org-ascii-code): (org-ascii-dynamic-block): (org-ascii-entity): (org-ascii-example-block): (org-ascii-export-snippet): (org-ascii-export-block): (org-ascii-fixed-width): (org-ascii-footnote-reference): (org-ascii-horizontal-rule): (org-ascii-inline-src-block): (org-ascii-format-inlinetask-default): (org-ascii-italic): (org-ascii-keyword): (org-ascii-latex-environment): (org-ascii-latex-fragment): (org-ascii-line-break): (org-ascii-node-property): (org-ascii-planning): (org-ascii-quote-block): (org-ascii-radio-target): (org-ascii-special-block): (org-ascii-src-block): (org-ascii-statistics-cookie): (org-ascii-subscript): (org-ascii-superscript): (org-ascii-strike-through): (org-ascii-timestamp): (org-ascii-underline): (org-ascii-verbatim): (org-ascii-verse-block): (org-ascii-filter-headline-blank-lines): (org-ascii-filter-paragraph-spacing): (org-ascii-filter-comment-spacing): Use lexical binding.
144 lines
5.4 KiB
EmacsLisp
144 lines
5.4 KiB
EmacsLisp
;;; ob-screen.el --- Babel Support for Interactive Terminal -*- lexical-binding: t; -*-
|
|
|
|
;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
|
|
|
|
;; Author: Benjamin Andresen
|
|
;; Keywords: literate programming, interactive shell
|
|
;; Homepage: http://orgmode.org
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
;; it under the terms of the GNU General Public License as published by
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
;; (at your option) any later version.
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
;; GNU General Public License for more details.
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
;;; Commentary:
|
|
|
|
;; Org-Babel support for interactive terminals. Mostly shell scripts.
|
|
;; Heavily inspired by 'eev' from Eduardo Ochs
|
|
;;
|
|
;; Adding :cmd and :terminal as header arguments
|
|
;; :terminal must support the -T (title) and -e (command) parameter
|
|
;;
|
|
;; You can test the default setup. (xterm + sh) with
|
|
;; M-x org-babel-screen-test RET
|
|
|
|
;;; Code:
|
|
(require 'ob)
|
|
|
|
(defvar org-babel-screen-location "screen"
|
|
"The command location for screen.
|
|
In case you want to use a different screen than one selected by your $PATH")
|
|
|
|
(defvar org-babel-default-header-args:screen
|
|
'((:results . "silent") (:session . "default") (:cmd . "sh") (:terminal . "xterm"))
|
|
"Default arguments to use when running screen source blocks.")
|
|
|
|
(defun org-babel-execute:screen (body params)
|
|
"Send a block of code via screen to a terminal using Babel.
|
|
\"default\" session is used when none is specified."
|
|
(message "Sending source code block to interactive terminal session...")
|
|
(save-window-excursion
|
|
(let* ((session (cdr (assoc :session params)))
|
|
(socket (org-babel-screen-session-socketname session)))
|
|
(unless socket (org-babel-prep-session:screen session params))
|
|
(org-babel-screen-session-execute-string
|
|
session (org-babel-expand-body:generic body params)))))
|
|
|
|
(defun org-babel-prep-session:screen (_session params)
|
|
"Prepare SESSION according to the header arguments specified in PARAMS."
|
|
(let* ((session (cdr (assoc :session params)))
|
|
(cmd (cdr (assoc :cmd params)))
|
|
(terminal (cdr (assoc :terminal params)))
|
|
(process-name (concat "org-babel: terminal (" session ")")))
|
|
(apply 'start-process process-name "*Messages*"
|
|
terminal `("-T" ,(concat "org-babel: " session) "-e" ,org-babel-screen-location
|
|
"-c" "/dev/null" "-mS" ,(concat "org-babel-session-" session)
|
|
,cmd))
|
|
;; XXX: Is there a better way than the following?
|
|
(while (not (org-babel-screen-session-socketname session))
|
|
;; wait until screen session is available before returning
|
|
)))
|
|
|
|
;; helper functions
|
|
|
|
(defun org-babel-screen-session-execute-string (session body)
|
|
"If SESSION exists, send BODY to it."
|
|
(let ((socket (org-babel-screen-session-socketname session)))
|
|
(when socket
|
|
(let ((tmpfile (org-babel-screen-session-write-temp-file session body)))
|
|
(apply 'start-process (concat "org-babel: screen (" session ")") "*Messages*"
|
|
org-babel-screen-location
|
|
`("-S" ,socket "-X" "eval" "msgwait 0"
|
|
,(concat "readreg z " tmpfile)
|
|
"paste z"))))))
|
|
|
|
(defun org-babel-screen-session-socketname (session)
|
|
"Check if SESSION exists by parsing output of \"screen -ls\"."
|
|
(let* ((screen-ls (shell-command-to-string "screen -ls"))
|
|
(sockets (delq
|
|
nil
|
|
(mapcar
|
|
(lambda (x)
|
|
(when (string-match (rx (or "(Attached)" "(Detached)")) x)
|
|
x))
|
|
(split-string screen-ls "\n"))))
|
|
(match-socket (car
|
|
(delq
|
|
nil
|
|
(mapcar
|
|
(lambda (x)
|
|
(when (string-match
|
|
(concat "org-babel-session-" session) x)
|
|
x))
|
|
sockets)))))
|
|
(when match-socket (car (split-string match-socket)))))
|
|
|
|
(defun org-babel-screen-session-write-temp-file (_session body)
|
|
"Save BODY in a temp file that is named after SESSION."
|
|
(let ((tmpfile (org-babel-temp-file "screen-")))
|
|
(with-temp-file tmpfile
|
|
(insert body)
|
|
|
|
;; org-babel has superfluous spaces
|
|
(goto-char (point-min))
|
|
(delete-matching-lines "^ +$"))
|
|
tmpfile))
|
|
|
|
(defun org-babel-screen-test ()
|
|
"Test if the default setup works.
|
|
The terminal should shortly flicker."
|
|
(interactive)
|
|
(let* ((random-string (format "%s" (random 99999)))
|
|
(tmpfile (org-babel-temp-file "ob-screen-test-"))
|
|
(body (concat "echo '" random-string "' > " tmpfile "\nexit\n"))
|
|
tmp-string)
|
|
(org-babel-execute:screen body org-babel-default-header-args:screen)
|
|
;; XXX: need to find a better way to do the following
|
|
(while (not (file-readable-p tmpfile))
|
|
;; do something, otherwise this will be optimized away
|
|
(format "org-babel-screen: File not readable yet."))
|
|
(setq tmp-string (with-temp-buffer
|
|
(insert-file-contents-literally tmpfile)
|
|
(buffer-substring (point-min) (point-max))))
|
|
(delete-file tmpfile)
|
|
(message (concat "org-babel-screen: Setup "
|
|
(if (string-match random-string tmp-string)
|
|
"WORKS."
|
|
"DOESN'T work.")))))
|
|
|
|
(provide 'ob-screen)
|
|
|
|
|
|
|
|
;;; ob-screen.el ends here
|