Commit | Line | Data |
---|---|---|
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 | ||
eb3fa2cf | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
0d20f9a0 | 14 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
0d20f9a0 JB |
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 | |
eb3fa2cf | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
0d20f9a0 | 25 | |
55535639 PJ |
26 | ;;; Commentary: |
27 | ||
e5167999 | 28 | ;;; Code: |
0d20f9a0 JB |
29 | |
30 | (defvar display-subprocess-window nil | |
40198478 | 31 | "If non-nil, the subprocess window is displayed whenever input is received.") |
0d20f9a0 JB |
32 | |
33 | (defvar command-prefix-string "$ " | |
34 | "String to insert to distinguish commands entered by user.") | |
35 | ||
36 | (defvar subprocess-running nil) | |
6bc52202 SM |
37 | (defvar subprocess-buf nil) |
38 | ||
ceee4f5d SM |
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)) | |
0d20f9a0 JB |
44 | |
45 | (defun subprocess-input (name str) | |
30cf0c33 | 46 | "Handle input from a subprocess. Called by Emacs." |
0d20f9a0 JB |
47 | (if display-subprocess-window |
48 | (display-buffer subprocess-buf)) | |
6bc52202 | 49 | (with-current-buffer subprocess-buf |
0d20f9a0 | 50 | (goto-char (point-max)) |
6bc52202 | 51 | (insert str ?\n))) |
0d20f9a0 JB |
52 | |
53 | (defun subprocess-exit (name) | |
54 | "Called by Emacs upon subprocess exit." | |
55 | (setq subprocess-running nil)) | |
56 | ||
57 | (defun start-subprocess () | |
30cf0c33 | 58 | "Spawn an asynchronous subprocess with output redirected to |
0d20f9a0 JB |
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*")) | |
ceee4f5d | 65 | (with-current-buffer subprocess-buf |
0d20f9a0 JB |
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"))) | |
73 | ||
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))) | |
ceee4f5d SM |
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)))) | |
0d20f9a0 JB |
98 | |
99 | (defun subprocess-command () | |
30cf0c33 | 100 | "Start asynchronous subprocess if not running and switch to its window." |
0d20f9a0 JB |
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))))) | |
106 | ||
107 | (defun command-send-input () | |
30cf0c33 JB |
108 | "If at last line of buffer, send the current line to |
109 | the spawned subprocess. Otherwise bring back current | |
0d20f9a0 JB |
110 | line to the last line for resubmission." |
111 | (interactive) | |
112 | (beginning-of-line) | |
ceee4f5d | 113 | (let ((current-line (buffer-substring (point) (line-end-position)))) |
0d20f9a0 JB |
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))) | |
97546017 | 124 | (forward-line 1)))) |
0d20f9a0 | 125 | ;; else -- if not at last line in buffer |
ceee4f5d | 126 | (goto-char (point-max)) |
0d20f9a0 | 127 | (backward-char) |
97546017 | 128 | (forward-line 1) |
ceee4f5d SM |
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))))) | |
0d20f9a0 | 134 | |
ceee4f5d | 135 | (defun command-kill-line () |
30cf0c33 | 136 | "Kill the current line. Used in command mode." |
0d20f9a0 JB |
137 | (interactive) |
138 | (beginning-of-line) | |
139 | (kill-line)) | |
140 | ||
141 | (define-key esc-map "$" 'subprocess-command) | |
d501f516 | 142 | |
97546017 DN |
143 | (provide 'vmsproc) |
144 | ||
ceee4f5d | 145 | ;; arch-tag: 600b2512-f903-4887-bcd2-e76b306f5b66 |
d501f516 | 146 | ;;; vmsproc.el ends here |