Initial revision
[emacs.git] / lisp / vmsproc.el
blobb4451a40ad0d3825edf7d865aaad9cda6b403b15
1 ;; Run asynchronous VMS subprocesses under Emacs
2 ;; Copyright (C) 1986 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
9 ;; any later version.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 ;; Written by Mukesh Prasad.
22 (defvar display-subprocess-window nil
23 "If non-nil, the suprocess window is displayed whenever input is received.")
25 (defvar command-prefix-string "$ "
26 "String to insert to distinguish commands entered by user.")
28 (defvar subprocess-running nil)
29 (defvar command-mode-map nil)
31 (if command-mode-map
32 nil
33 (setq command-mode-map (make-sparse-keymap))
34 (define-key command-mode-map "\C-m" 'command-send-input)
35 (define-key command-mode-map "\C-u" 'command-kill-line))
37 (defun subprocess-input (name str)
38 "Handles input from a subprocess. Called by Emacs."
39 (if display-subprocess-window
40 (display-buffer subprocess-buf))
41 (let ((old-buffer (current-buffer)))
42 (set-buffer subprocess-buf)
43 (goto-char (point-max))
44 (insert str)
45 (insert ?\n)
46 (set-buffer old-buffer)))
48 (defun subprocess-exit (name)
49 "Called by Emacs upon subprocess exit."
50 (setq subprocess-running nil))
52 (defun start-subprocess ()
53 "Spawns an asynchronous subprocess with output redirected to
54 the buffer *COMMAND*. Within this buffer, use C-m to send
55 the last line to the subprocess or to bring another line to
56 the end."
57 (if subprocess-running
58 (return t))
59 (setq subprocess-buf (get-buffer-create "*COMMAND*"))
60 (save-excursion
61 (set-buffer subprocess-buf)
62 (use-local-map command-mode-map))
63 (setq subprocess-running (spawn-subprocess 1 'subprocess-input
64 'subprocess-exit))
65 ;; Initialize subprocess so it doesn't panic and die upon
66 ;; encountering the first error.
67 (and subprocess-running
68 (send-command-to-subprocess 1 "ON SEVERE_ERROR THEN CONTINUE")))
70 (defun subprocess-command-to-buffer (command buffer)
71 "Execute COMMAND and redirect output into BUFFER."
72 (let (cmd args)
73 (setq cmd (substring command 0 (string-match " " command)))
74 (setq args (substring command (string-match " " command)))
75 (call-process cmd nil buffer nil "*dcl*" args)))
76 ;BUGS: only the output up to the end of the first image activation is trapped.
77 ; (if (not subprocess-running)
78 ; (start-subprocess))
79 ; (save-excursion
80 ; (set-buffer buffer)
81 ; (let ((output-filename (concat "SYS$SCRATCH:OUTPUT-FOR-"
82 ; (getenv "USER") ".LISTING")))
83 ; (while (file-exists-p output-filename)
84 ; (delete-file output-filename))
85 ; (define-logical-name "SYS$OUTPUT" (concat output-filename "-NEW"))
86 ; (send-command-to-subprocess 1 command)
87 ; (send-command-to-subprocess 1 (concat
88 ; "RENAME " output-filename
89 ; "-NEW " output-filename))
90 ; (while (not (file-exists-p output-filename))
91 ; (sleep-for 1))
92 ; (define-logical-name "SYS$OUTPUT" nil)
93 ; (insert-file output-filename)
94 ; (delete-file output-filename))))
96 (defun subprocess-command ()
97 "Starts asynchronous subprocess if not running and switches to its window."
98 (interactive)
99 (if (not subprocess-running)
100 (start-subprocess))
101 (and subprocess-running
102 (progn (pop-to-buffer subprocess-buf) (goto-char (point-max)))))
104 (defun command-send-input ()
105 "If at last line of buffer, sends the current line to
106 the spawned subprocess. Otherwise brings back current
107 line to the last line for resubmission."
108 (interactive)
109 (beginning-of-line)
110 (let ((current-line (buffer-substring (point)
111 (progn (end-of-line) (point)))))
112 (if (eobp)
113 (progn
114 (if (not subprocess-running)
115 (start-subprocess))
116 (if subprocess-running
117 (progn
118 (beginning-of-line)
119 (send-command-to-subprocess 1 current-line)
120 (if command-prefix-string
121 (progn (beginning-of-line) (insert command-prefix-string)))
122 (next-line 1))))
123 ;; else -- if not at last line in buffer
124 (end-of-buffer)
125 (backward-char)
126 (next-line 1)
127 (if (string-equal command-prefix-string
128 (substring current-line 0 (length command-prefix-string)))
129 (insert (substring current-line (length command-prefix-string)))
130 (insert current-line)))))
132 (defun command-kill-line()
133 "Kills the current line. Used in command mode."
134 (interactive)
135 (beginning-of-line)
136 (kill-line))
138 (define-key esc-map "$" 'subprocess-command)