Rob Riepel <riepel at networking.stanford.edu>
[emacs.git] / lisp / vmsproc.el
blob92df327dde1c098fb88509302d795212c29e852e
1 ;;; vmsproc.el --- run asynchronous VMS subprocesses under Emacs
3 ;; Copyright (C) 1986, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Mukesh Prasad
7 ;; Maintainer: FSF
8 ;; Keywords: vms
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, or (at your option)
15 ;; 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; 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 ;;; Code:
31 (defvar display-subprocess-window nil
32 "If non-nil, the subprocess window is displayed whenever input is received.")
34 (defvar command-prefix-string "$ "
35 "String to insert to distinguish commands entered by user.")
37 (defvar subprocess-running nil)
38 (defvar subprocess-buf nil)
40 (defvar command-mode-map
41 (let ((map (make-sparse-keymap)))
42 (define-key map "\C-m" 'command-send-input)
43 (define-key map "\C-u" 'command-kill-line)
44 map))
46 (defun subprocess-input (name str)
47 "Handle input from a subprocess. Called by Emacs."
48 (if display-subprocess-window
49 (display-buffer subprocess-buf))
50 (with-current-buffer subprocess-buf
51 (goto-char (point-max))
52 (insert str ?\n)))
54 (defun subprocess-exit (name)
55 "Called by Emacs upon subprocess exit."
56 (setq subprocess-running nil))
58 (defun start-subprocess ()
59 "Spawn an asynchronous subprocess with output redirected to
60 the buffer *COMMAND*. Within this buffer, use C-m to send
61 the last line to the subprocess or to bring another line to
62 the end."
63 (if subprocess-running
64 (return t))
65 (setq subprocess-buf (get-buffer-create "*COMMAND*"))
66 (with-current-buffer subprocess-buf
67 (use-local-map command-mode-map))
68 (setq subprocess-running (spawn-subprocess 1 'subprocess-input
69 'subprocess-exit))
70 ;; Initialize subprocess so it doesn't panic and die upon
71 ;; encountering the first error.
72 (and subprocess-running
73 (send-command-to-subprocess 1 "ON SEVERE_ERROR THEN CONTINUE")))
75 (defun subprocess-command-to-buffer (command buffer)
76 "Execute COMMAND and redirect output into BUFFER."
77 (let (cmd args)
78 (setq cmd (substring command 0 (string-match " " command)))
79 (setq args (substring command (string-match " " command)))
80 (call-process cmd nil buffer nil "*dcl*" args)))
81 ;; BUGS: only the output up to the end of the first image activation is trapped.
82 ;; (if (not subprocess-running)
83 ;; (start-subprocess))
84 ;; (with-current-buffer buffer
85 ;; (let ((output-filename (concat "SYS$SCRATCH:OUTPUT-FOR-"
86 ;; (getenv "USER") ".LISTING")))
87 ;; (while (file-exists-p output-filename)
88 ;; (delete-file output-filename))
89 ;; (define-logical-name "SYS$OUTPUT" (concat output-filename "-NEW"))
90 ;; (send-command-to-subprocess 1 command)
91 ;; (send-command-to-subprocess 1 (concat
92 ;; "RENAME " output-filename
93 ;; "-NEW " output-filename))
94 ;; (while (not (file-exists-p output-filename))
95 ;; (sleep-for 1))
96 ;; (define-logical-name "SYS$OUTPUT" nil)
97 ;; (insert-file output-filename)
98 ;; (delete-file output-filename))))
100 (defun subprocess-command ()
101 "Start asynchronous subprocess if not running and switch to its window."
102 (interactive)
103 (if (not subprocess-running)
104 (start-subprocess))
105 (and subprocess-running
106 (progn (pop-to-buffer subprocess-buf) (goto-char (point-max)))))
108 (defun command-send-input ()
109 "If at last line of buffer, send the current line to
110 the spawned subprocess. Otherwise bring back current
111 line to the last line for resubmission."
112 (interactive)
113 (beginning-of-line)
114 (let ((current-line (buffer-substring (point) (line-end-position))))
115 (if (eobp)
116 (progn
117 (if (not subprocess-running)
118 (start-subprocess))
119 (if subprocess-running
120 (progn
121 (beginning-of-line)
122 (send-command-to-subprocess 1 current-line)
123 (if command-prefix-string
124 (progn (beginning-of-line) (insert command-prefix-string)))
125 (forward-line 1))))
126 ;; else -- if not at last line in buffer
127 (goto-char (point-max))
128 (backward-char)
129 (forward-line 1)
130 (insert
131 (if (compare-strings command-prefix-string nil nil
132 current-line 0 (length command-prefix-string))
133 (substring current-line (length command-prefix-string))
134 current-line)))))
136 (defun command-kill-line ()
137 "Kill the current line. Used in command mode."
138 (interactive)
139 (beginning-of-line)
140 (kill-line))
142 (define-key esc-map "$" 'subprocess-command)
144 (provide 'vmsproc)
146 ;; arch-tag: 600b2512-f903-4887-bcd2-e76b306f5b66
147 ;;; vmsproc.el ends here