]> code.delx.au - gnu-emacs/blob - lisp/vmsx.el
Initial revision
[gnu-emacs] / lisp / vmsx.el
1 ;; Run asynchronous VMS subprocesses under Emacs
2 ;; Copyright (C) 1986 Free Software Foundation, Inc.
3
4 ;; This file is part of GNU Emacs.
5
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.
10
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.
15
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.
19
20 ;; Written by Mukesh Prasad.
21
22 (defvar display-subprocess-window nil
23 "If non-nil, the suprocess window is displayed whenever input is received.")
24
25 (defvar command-prefix-string "$ "
26 "String to insert to distinguish commands entered by user.")
27
28 (defvar subprocess-running nil)
29 (defvar command-mode-map nil)
30
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))
36
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)))
47
48 (defun subprocess-exit (name)
49 "Called by Emacs upon subprocess exit."
50 (setq subprocess-running nil))
51
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")))
69
70 (defvar subprocess-command-to-buffer-tmpdir "SYS$SCRATCH:"
71 "*Put temporary files from subprocess-command-to-buffer here.")
72
73 (defun subprocess-command-to-buffer (command buffer)
74 "Execute command and redirect output into buffer.
75
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
82 (concat subprocess-command-to-buffer-tmpdir
83 "OUTPUT-FOR-" (getenv "USER") ".LISTING")))
84 (while (file-attributes output-filename)
85 (delete-file output-filename))
86 (send-command-to-subprocess 1 (concat "DEFINE/USER SYS$OUTPUT "
87 output-filename "-NEW"))
88 (send-command-to-subprocess 1 command)
89 (send-command-to-subprocess 1 (concat "RENAME " output-filename
90 "-NEW " output-filename))
91 (while (not (file-attributes output-filename))
92 (sleep-for 2))
93 (insert-file output-filename))))
94
95 (defun subprocess-command ()
96 "Starts asynchronous subprocess if not running and switches to its window."
97 (interactive)
98 (if (not subprocess-running)
99 (start-subprocess))
100 (and subprocess-running
101 (progn (pop-to-buffer subprocess-buf) (goto-char (point-max)))))
102
103 (defun command-send-input ()
104 "If at last line of buffer, sends the current line to
105 the spawned subprocess. Otherwise brings back current
106 line to the last line for resubmission."
107 (interactive)
108 (beginning-of-line)
109 (let ((current-line (buffer-substring (point)
110 (progn (end-of-line) (point)))))
111 (if (eobp)
112 (progn
113 (if (not subprocess-running)
114 (start-subprocess))
115 (if subprocess-running
116 (progn
117 (beginning-of-line)
118 (send-command-to-subprocess 1 current-line)
119 (if command-prefix-string
120 (progn (beginning-of-line) (insert command-prefix-string)))
121 (next-line 1))))
122 ;; else -- if not at last line in buffer
123 (end-of-buffer)
124 (backward-char)
125 (next-line 1)
126 (if (string-equal command-prefix-string
127 (substring current-line 0 (length command-prefix-string)))
128 (insert (substring current-line (length command-prefix-string)))
129 (insert current-line)))))
130
131 (defun command-kill-line()
132 "Kills the current line. Used in command mode."
133 (interactive)
134 (beginning-of-line)
135 (kill-line))
136
137 (define-key esc-map "$" 'subprocess-command)