Commit | Line | Data |
---|---|---|
e5167999 ER |
1 | ;;; vmsproc.el --- run asynchronous VMS subprocesses under Emacs |
2 | ||
0d30b337 | 3 | ;; Copyright (C) 1986, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
58142744 | 4 | |
e5167999 ER |
5 | ;; Author: Mukesh Prasad |
6 | ;; Maintainer: FSF | |
6251ee24 | 7 | ;; Keywords: vms |
76d7458e | 8 | |
0d20f9a0 JB |
9 | ;; This file is part of GNU Emacs. |
10 | ||
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 | |
e5167999 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
0d20f9a0 JB |
14 | ;; any later version. |
15 | ||
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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 | 22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
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) | |
46 | "Handles input from a subprocess. Called by Emacs." | |
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 () | |
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*")) | |
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 () | |
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))))) | |
106 | ||
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) | |
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))) | |
124 | (next-line 1)))) | |
125 | ;; else -- if not at last line in buffer | |
ceee4f5d | 126 | (goto-char (point-max)) |
0d20f9a0 JB |
127 | (backward-char) |
128 | (next-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 () |
0d20f9a0 JB |
136 | "Kills the current line. Used in command mode." |
137 | (interactive) | |
138 | (beginning-of-line) | |
139 | (kill-line)) | |
140 | ||
141 | (define-key esc-map "$" 'subprocess-command) | |
d501f516 | 142 | |
ceee4f5d | 143 | ;; arch-tag: 600b2512-f903-4887-bcd2-e76b306f5b66 |
d501f516 | 144 | ;;; vmsproc.el ends here |