Large simplification in (vc-deduce-fileset) logic.
[emacs.git] / lisp / vmsproc.el
blob6d7fcf9866543b062b232a4b4ab54246dc09be30
1 ;; -*- no-byte-compile: t -*-
2 ;;; vmsproc.el --- run asynchronous VMS subprocesses under Emacs
4 ;; Copyright (C) 1986, 2001, 2002, 2003, 2004, 2005,
5 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
7 ;; Author: Mukesh Prasad
8 ;; Maintainer: FSF
9 ;; Keywords: vms
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 "Handle 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 "Spawn 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 "Start asynchronous subprocess if not running and switch 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, send the current line to
109 the spawned subprocess. Otherwise bring 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 (forward-line 1))))
125 ;; else -- if not at last line in buffer
126 (goto-char (point-max))
127 (backward-char)
128 (forward-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 "Kill the current line. Used in command mode."
137 (interactive)
138 (beginning-of-line)
139 (kill-line))
141 (define-key esc-map "$" 'subprocess-command)
143 (provide 'vmsproc)
145 ;; arch-tag: 600b2512-f903-4887-bcd2-e76b306f5b66
146 ;;; vmsproc.el ends here