Use find-file-hook instead of find-file-hooks.
[bpt/emacs.git] / lisp / vmsproc.el
CommitLineData
73e72da4 1;; -*- no-byte-compile: t -*-
e5167999
ER
2;;; vmsproc.el --- run asynchronous VMS subprocesses under Emacs
3
c90f2757 4;; Copyright (C) 1986, 2001, 2002, 2003, 2004, 2005,
409cc4a3 5;; 2006, 2007, 2008 Free Software Foundation, Inc.
58142744 6
e5167999
ER
7;; Author: Mukesh Prasad
8;; Maintainer: FSF
6251ee24 9;; Keywords: vms
76d7458e 10
0d20f9a0
JB
11;; This file is part of GNU Emacs.
12
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
b4aa6026 15;; the Free Software Foundation; either version 3, or (at your option)
0d20f9a0
JB
16;; any later version.
17
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.
22
23;; You should have received a copy of the GNU General Public License
b578f267 24;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
0d20f9a0 27
55535639
PJ
28;;; Commentary:
29
e5167999 30;;; Code:
0d20f9a0
JB
31
32(defvar display-subprocess-window nil
40198478 33 "If non-nil, the subprocess window is displayed whenever input is received.")
0d20f9a0
JB
34
35(defvar command-prefix-string "$ "
36 "String to insert to distinguish commands entered by user.")
37
38(defvar subprocess-running nil)
6bc52202
SM
39(defvar subprocess-buf nil)
40
ceee4f5d
SM
41(defvar command-mode-map
42 (let ((map (make-sparse-keymap)))
43 (define-key map "\C-m" 'command-send-input)
44 (define-key map "\C-u" 'command-kill-line)
45 map))
0d20f9a0
JB
46
47(defun subprocess-input (name str)
30cf0c33 48 "Handle input from a subprocess. Called by Emacs."
0d20f9a0
JB
49 (if display-subprocess-window
50 (display-buffer subprocess-buf))
6bc52202 51 (with-current-buffer subprocess-buf
0d20f9a0 52 (goto-char (point-max))
6bc52202 53 (insert str ?\n)))
0d20f9a0
JB
54
55(defun subprocess-exit (name)
56 "Called by Emacs upon subprocess exit."
57 (setq subprocess-running nil))
58
59(defun start-subprocess ()
30cf0c33 60 "Spawn an asynchronous subprocess with output redirected to
0d20f9a0
JB
61the buffer *COMMAND*. Within this buffer, use C-m to send
62the last line to the subprocess or to bring another line to
63the end."
64 (if subprocess-running
65 (return t))
66 (setq subprocess-buf (get-buffer-create "*COMMAND*"))
ceee4f5d 67 (with-current-buffer subprocess-buf
0d20f9a0
JB
68 (use-local-map command-mode-map))
69 (setq subprocess-running (spawn-subprocess 1 'subprocess-input
70 'subprocess-exit))
71 ;; Initialize subprocess so it doesn't panic and die upon
72 ;; encountering the first error.
73 (and subprocess-running
74 (send-command-to-subprocess 1 "ON SEVERE_ERROR THEN CONTINUE")))
75
76(defun subprocess-command-to-buffer (command buffer)
77 "Execute COMMAND and redirect output into BUFFER."
78 (let (cmd args)
79 (setq cmd (substring command 0 (string-match " " command)))
80 (setq args (substring command (string-match " " command)))
81 (call-process cmd nil buffer nil "*dcl*" args)))
ceee4f5d
SM
82 ;; BUGS: only the output up to the end of the first image activation is trapped.
83 ;; (if (not subprocess-running)
84 ;; (start-subprocess))
85 ;; (with-current-buffer buffer
86 ;; (let ((output-filename (concat "SYS$SCRATCH:OUTPUT-FOR-"
87 ;; (getenv "USER") ".LISTING")))
88 ;; (while (file-exists-p output-filename)
89 ;; (delete-file output-filename))
90 ;; (define-logical-name "SYS$OUTPUT" (concat output-filename "-NEW"))
91 ;; (send-command-to-subprocess 1 command)
92 ;; (send-command-to-subprocess 1 (concat
93 ;; "RENAME " output-filename
94 ;; "-NEW " output-filename))
95 ;; (while (not (file-exists-p output-filename))
96 ;; (sleep-for 1))
97 ;; (define-logical-name "SYS$OUTPUT" nil)
98 ;; (insert-file output-filename)
99 ;; (delete-file output-filename))))
0d20f9a0
JB
100
101(defun subprocess-command ()
30cf0c33 102 "Start asynchronous subprocess if not running and switch to its window."
0d20f9a0
JB
103 (interactive)
104 (if (not subprocess-running)
105 (start-subprocess))
106 (and subprocess-running
107 (progn (pop-to-buffer subprocess-buf) (goto-char (point-max)))))
108
109(defun command-send-input ()
30cf0c33
JB
110 "If at last line of buffer, send the current line to
111the spawned subprocess. Otherwise bring back current
0d20f9a0
JB
112line to the last line for resubmission."
113 (interactive)
114 (beginning-of-line)
ceee4f5d 115 (let ((current-line (buffer-substring (point) (line-end-position))))
0d20f9a0
JB
116 (if (eobp)
117 (progn
118 (if (not subprocess-running)
119 (start-subprocess))
120 (if subprocess-running
121 (progn
122 (beginning-of-line)
123 (send-command-to-subprocess 1 current-line)
124 (if command-prefix-string
125 (progn (beginning-of-line) (insert command-prefix-string)))
97546017 126 (forward-line 1))))
0d20f9a0 127 ;; else -- if not at last line in buffer
ceee4f5d 128 (goto-char (point-max))
0d20f9a0 129 (backward-char)
97546017 130 (forward-line 1)
ceee4f5d
SM
131 (insert
132 (if (compare-strings command-prefix-string nil nil
133 current-line 0 (length command-prefix-string))
134 (substring current-line (length command-prefix-string))
135 current-line)))))
0d20f9a0 136
ceee4f5d 137(defun command-kill-line ()
30cf0c33 138 "Kill the current line. Used in command mode."
0d20f9a0
JB
139 (interactive)
140 (beginning-of-line)
141 (kill-line))
142
143(define-key esc-map "$" 'subprocess-command)
d501f516 144
97546017
DN
145(provide 'vmsproc)
146
ceee4f5d 147;; arch-tag: 600b2512-f903-4887-bcd2-e76b306f5b66
d501f516 148;;; vmsproc.el ends here