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