Sync to HEAD
[bpt/emacs.git] / lisp / vmsproc.el
CommitLineData
e5167999
ER
1;;; vmsproc.el --- run asynchronous VMS subprocesses under Emacs
2
58142744
ER
3;; Copyright (C) 1986 Free Software Foundation, Inc.
4
e5167999
ER
5;; Author: Mukesh Prasad
6;; Maintainer: FSF
6251ee24 7;; Keywords: vms
76d7458e 8
0d20f9a0
JB
9;; This file is part of GNU Emacs.
10
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
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
0d20f9a0
JB
14;; any later version.
15
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.
20
21;; You should have received a copy of the GNU General Public License
b578f267
EN
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
0d20f9a0 25
55535639
PJ
26;;; Commentary:
27
e5167999 28;;; Code:
0d20f9a0
JB
29
30(defvar display-subprocess-window nil
40198478 31 "If non-nil, the subprocess window is displayed whenever input is received.")
0d20f9a0
JB
32
33(defvar command-prefix-string "$ "
34 "String to insert to distinguish commands entered by user.")
35
36(defvar subprocess-running nil)
37(defvar command-mode-map nil)
38
39(if command-mode-map
40 nil
41 (setq command-mode-map (make-sparse-keymap))
42 (define-key command-mode-map "\C-m" 'command-send-input)
43 (define-key command-mode-map "\C-u" 'command-kill-line))
44
45(defun subprocess-input (name str)
46 "Handles input from a subprocess. Called by Emacs."
47 (if display-subprocess-window
48 (display-buffer subprocess-buf))
49 (let ((old-buffer (current-buffer)))
50 (set-buffer subprocess-buf)
51 (goto-char (point-max))
52 (insert str)
53 (insert ?\n)
54 (set-buffer old-buffer)))
55
56(defun subprocess-exit (name)
57 "Called by Emacs upon subprocess exit."
58 (setq subprocess-running nil))
59
60(defun start-subprocess ()
61 "Spawns an asynchronous subprocess with output redirected to
62the buffer *COMMAND*. Within this buffer, use C-m to send
63the last line to the subprocess or to bring another line to
64the end."
65 (if subprocess-running
66 (return t))
67 (setq subprocess-buf (get-buffer-create "*COMMAND*"))
68 (save-excursion
69 (set-buffer subprocess-buf)
70 (use-local-map command-mode-map))
71 (setq subprocess-running (spawn-subprocess 1 'subprocess-input
72 'subprocess-exit))
73 ;; Initialize subprocess so it doesn't panic and die upon
74 ;; encountering the first error.
75 (and subprocess-running
76 (send-command-to-subprocess 1 "ON SEVERE_ERROR THEN CONTINUE")))
77
78(defun subprocess-command-to-buffer (command buffer)
79 "Execute COMMAND and redirect output into BUFFER."
80 (let (cmd args)
81 (setq cmd (substring command 0 (string-match " " command)))
82 (setq args (substring command (string-match " " command)))
83 (call-process cmd nil buffer nil "*dcl*" args)))
84;BUGS: only the output up to the end of the first image activation is trapped.
85; (if (not subprocess-running)
86; (start-subprocess))
87; (save-excursion
88; (set-buffer buffer)
89; (let ((output-filename (concat "SYS$SCRATCH:OUTPUT-FOR-"
90; (getenv "USER") ".LISTING")))
91; (while (file-exists-p output-filename)
92; (delete-file output-filename))
93; (define-logical-name "SYS$OUTPUT" (concat output-filename "-NEW"))
94; (send-command-to-subprocess 1 command)
95; (send-command-to-subprocess 1 (concat
96; "RENAME " output-filename
97; "-NEW " output-filename))
98; (while (not (file-exists-p output-filename))
99; (sleep-for 1))
100; (define-logical-name "SYS$OUTPUT" nil)
101; (insert-file output-filename)
102; (delete-file output-filename))))
103
104(defun subprocess-command ()
105 "Starts asynchronous subprocess if not running and switches to its window."
106 (interactive)
107 (if (not subprocess-running)
108 (start-subprocess))
109 (and subprocess-running
110 (progn (pop-to-buffer subprocess-buf) (goto-char (point-max)))))
111
112(defun command-send-input ()
113 "If at last line of buffer, sends the current line to
114the spawned subprocess. Otherwise brings back current
115line to the last line for resubmission."
116 (interactive)
117 (beginning-of-line)
118 (let ((current-line (buffer-substring (point)
119 (progn (end-of-line) (point)))))
120 (if (eobp)
121 (progn
122 (if (not subprocess-running)
123 (start-subprocess))
124 (if subprocess-running
125 (progn
126 (beginning-of-line)
127 (send-command-to-subprocess 1 current-line)
128 (if command-prefix-string
129 (progn (beginning-of-line) (insert command-prefix-string)))
130 (next-line 1))))
131 ;; else -- if not at last line in buffer
132 (end-of-buffer)
133 (backward-char)
134 (next-line 1)
135 (if (string-equal command-prefix-string
136 (substring current-line 0 (length command-prefix-string)))
137 (insert (substring current-line (length command-prefix-string)))
138 (insert current-line)))))
139
140(defun command-kill-line()
141 "Kills the current line. Used in command mode."
142 (interactive)
143 (beginning-of-line)
144 (kill-line))
145
146(define-key esc-map "$" 'subprocess-command)
d501f516 147
6b61353c 148;;; arch-tag: 600b2512-f903-4887-bcd2-e76b306f5b66
d501f516 149;;; vmsproc.el ends here