Update years in copyright notice; nfc.
[bpt/emacs.git] / lisp / vmsproc.el
CommitLineData
e5167999
ER
1;;; vmsproc.el --- run asynchronous VMS subprocesses under Emacs
2
aaef169d
TTN
3;; Copyright (C) 1986, 2002, 2003, 2004, 2005,
4;; 2006 Free Software Foundation, Inc.
58142744 5
e5167999
ER
6;; Author: Mukesh Prasad
7;; Maintainer: FSF
6251ee24 8;; Keywords: vms
76d7458e 9
0d20f9a0
JB
10;; This file is part of GNU Emacs.
11
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
e5167999 14;; the Free Software Foundation; either version 2, or (at your option)
0d20f9a0
JB
15;; any later version.
16
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.
21
22;; You should have received a copy of the GNU General Public License
b578f267 23;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
0d20f9a0 26
55535639
PJ
27;;; Commentary:
28
e5167999 29;;; Code:
0d20f9a0
JB
30
31(defvar display-subprocess-window nil
40198478 32 "If non-nil, the subprocess window is displayed whenever input is received.")
0d20f9a0
JB
33
34(defvar command-prefix-string "$ "
35 "String to insert to distinguish commands entered by user.")
36
37(defvar subprocess-running nil)
6bc52202
SM
38(defvar subprocess-buf nil)
39
ceee4f5d
SM
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))
0d20f9a0
JB
45
46(defun subprocess-input (name str)
47 "Handles input from a subprocess. Called by Emacs."
48 (if display-subprocess-window
49 (display-buffer subprocess-buf))
6bc52202 50 (with-current-buffer subprocess-buf
0d20f9a0 51 (goto-char (point-max))
6bc52202 52 (insert str ?\n)))
0d20f9a0
JB
53
54(defun subprocess-exit (name)
55 "Called by Emacs upon subprocess exit."
56 (setq subprocess-running nil))
57
58(defun start-subprocess ()
59 "Spawns an asynchronous subprocess with output redirected to
60the buffer *COMMAND*. Within this buffer, use C-m to send
61the last line to the subprocess or to bring another line to
62the end."
63 (if subprocess-running
64 (return t))
65 (setq subprocess-buf (get-buffer-create "*COMMAND*"))
ceee4f5d 66 (with-current-buffer subprocess-buf
0d20f9a0
JB
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")))
74
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)))
ceee4f5d
SM
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))))
0d20f9a0
JB
99
100(defun subprocess-command ()
101 "Starts asynchronous subprocess if not running and switches 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)))))
107
108(defun command-send-input ()
109 "If at last line of buffer, sends the current line to
110the spawned subprocess. Otherwise brings back current
111line to the last line for resubmission."
112 (interactive)
113 (beginning-of-line)
ceee4f5d 114 (let ((current-line (buffer-substring (point) (line-end-position))))
0d20f9a0
JB
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 (next-line 1))))
126 ;; else -- if not at last line in buffer
ceee4f5d 127 (goto-char (point-max))
0d20f9a0
JB
128 (backward-char)
129 (next-line 1)
ceee4f5d
SM
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)))))
0d20f9a0 135
ceee4f5d 136(defun command-kill-line ()
0d20f9a0
JB
137 "Kills the current line. Used in command mode."
138 (interactive)
139 (beginning-of-line)
140 (kill-line))
141
142(define-key esc-map "$" 'subprocess-command)
d501f516 143
ceee4f5d 144;; arch-tag: 600b2512-f903-4887-bcd2-e76b306f5b66
d501f516 145;;; vmsproc.el ends here