annotate lisp/process.el @ 377:d883f39b8495 r21-2b4

Import from CVS: tag r21-2b4
author cvs
date Mon, 13 Aug 2007 11:05:42 +0200
parents cc15677e0335
children 8626e4521993
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1 ;;; process.el --- commands for subprocesses; split out of simple.el
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
2
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
3 ;; Copyright (C) 1985-7, 1993,4, 1997 Free Software Foundation, Inc.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
4 ;; Copyright (C) 1995 Ben Wing.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
5
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
6 ;; Author: Ben Wing
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
7 ;; Maintainer: XEmacs Development Team
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
8 ;; Keywords: internal, processes, dumped
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
9
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
10 ;; This file is part of XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
11
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
12 ;; XEmacs is free software; you can redistribute it and/or modify it
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
13 ;; under the terms of the GNU General Public License as published by
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
15 ;; any later version.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
16
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
17 ;; XEmacs is distributed in the hope that it will be useful, but
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
20 ;; General Public License for more details.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
21
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
23 ;; along with XEmacs; see the file COPYING. If not, write to the
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
24 ;; Free Software Foundation, 59 Temple Place - Suite 330,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
25 ;; Boston, MA 02111-1307, USA.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
26
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
27 ;;; Synched up with: FSF 19.30.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
28
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
29 ;;; Commentary:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
30
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
31 ;; This file is dumped with XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
32
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
33 ;;; Code:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
34
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
35
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
36 (defgroup processes nil
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
37 "Process, subshell, compilation, and job control support."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
38 :group 'external
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
39 :group 'development)
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
40
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
41 (defgroup processes-basics nil
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
42 "Basic stuff dealing with processes."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
43 :group 'processes)
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
44
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
45 (defgroup execute nil
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
46 "Executing external commands."
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
47 :group 'processes)
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
48
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
49
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
50 (defvar shell-command-switch "-c"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
51 "Switch used to have the shell execute its command line argument.")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
52
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
53 (defun start-process-shell-command (name buffer &rest args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
54 "Start a program in a subprocess. Return the process object for it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
55 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
56 NAME is name for process. It is modified if necessary to make it unique.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
57 BUFFER is the buffer or (buffer-name) to associate with the process.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
58 Process output goes at end of that buffer, unless you specify
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
59 an output stream or filter function to handle the output.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
60 BUFFER may be also nil, meaning that this process is not associated
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
61 with any buffer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
62 Third arg is command name, the name of a shell command.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
63 Remaining arguments are the arguments for the command.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
64 Wildcards and redirection are handled as usual in the shell."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
65 (cond
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
66 ((eq system-type 'vax-vms)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
67 (apply 'start-process name buffer args))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
68 ;; We used to use `exec' to replace the shell with the command,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
69 ;; but that failed to handle (...) and semicolon, etc.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
70 (t
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
71 (start-process name buffer shell-file-name shell-command-switch
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
72 (mapconcat 'identity args " ")))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
73
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
74 (defun call-process (program &optional infile buffer displayp &rest args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
75 "Call PROGRAM synchronously in separate process.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
76 The program's input comes from file INFILE (nil means `/dev/null').
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
77 Insert output in BUFFER before point; t means current buffer;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
78 nil for BUFFER means discard it; 0 means discard and don't wait.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
79 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
80 REAL-BUFFER says what to do with standard output, as above,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
81 while STDERR-FILE says what to do with standard error in the child.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
82 STDERR-FILE may be nil (discard standard error output),
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
83 t (mix it with ordinary output), or a file name string.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
84
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
85 Fourth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
86 Remaining arguments are strings passed as command arguments to PROGRAM.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
87
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
88 If BUFFER is 0, `call-process' returns immediately with value nil.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
89 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
90 or a signal description string.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
91 If you quit, the process is killed with SIGINT, or SIGKILL if you
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
92 quit again."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
93 (apply 'call-process-internal program infile buffer displayp args))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
94
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
95 (defun call-process-region (start end program
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
96 &optional deletep buffer displayp
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
97 &rest args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
98 "Send text from START to END to a synchronous process running PROGRAM.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
99 Delete the text if fourth arg DELETEP is non-nil.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
100
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
101 Insert output in BUFFER before point; t means current buffer;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
102 nil for BUFFER means discard it; 0 means discard and don't wait.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
103 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
104 REAL-BUFFER says what to do with standard output, as above,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
105 while STDERR-FILE says what to do with standard error in the child.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
106 STDERR-FILE may be nil (discard standard error output),
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
107 t (mix it with ordinary output), or a file name string.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
108
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
109 Sixth arg DISPLAYP non-nil means redisplay buffer as output is inserted.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
110 Remaining args are passed to PROGRAM at startup as command args.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
111
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
112 If BUFFER is 0, returns immediately with value nil.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
113 Otherwise waits for PROGRAM to terminate
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
114 and returns a numeric exit status or a signal description string.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
115 If you quit, the process is first killed with SIGINT, then with SIGKILL if
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
116 you quit again before the process exits."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
117 (let ((temp (cond ((eq system-type 'vax-vms)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
118 (make-temp-name "tmp:emacs"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
119 ((or (eq system-type 'ms-dos)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
120 (eq system-type 'windows-nt))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
121 (make-temp-name
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
122 (concat (file-name-as-directory
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 227
diff changeset
123 (temp-directory))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
124 "em")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
125 (t
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 227
diff changeset
126 (make-temp-name
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 227
diff changeset
127 (concat (file-name-as-directory
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 227
diff changeset
128 (temp-directory))
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 227
diff changeset
129 "emacs"))))))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
130 (unwind-protect
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
131 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
132 (if (or (eq system-type 'ms-dos)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
133 (eq system-type 'windows-nt))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
134 (let ((buffer-file-type binary-process-output))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
135 (write-region start end temp nil 'silent))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
136 (write-region start end temp nil 'silent))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
137 (if deletep (delete-region start end))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
138 (apply #'call-process program temp buffer displayp args))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
139 (condition-case ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
140 (delete-file temp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
141 (file-error nil)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
142
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
143
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
144 (defun shell-command (command &optional output-buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
145 "Execute string COMMAND in inferior shell; display output, if any.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
146
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
147 If COMMAND ends in ampersand, execute it asynchronously.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
148 The output appears in the buffer `*Async Shell Command*'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
149 That buffer is in shell mode.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
150
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
151 Otherwise, COMMAND is executed synchronously. The output appears in the
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
152 buffer `*Shell Command Output*'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
153 If the output is one line, it is displayed in the echo area *as well*,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
154 but it is nonetheless available in buffer `*Shell Command Output*',
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
155 even though that buffer is not automatically displayed.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
156 If there is no output, or if output is inserted in the current buffer,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
157 then `*Shell Command Output*' is deleted.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
158
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
159 The optional second argument OUTPUT-BUFFER, if non-nil,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
160 says to put the output in some other buffer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
161 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
162 If OUTPUT-BUFFER is not a buffer and not nil,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
163 insert output in current buffer. (This cannot be done asynchronously.)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
164 In either case, the output is inserted after point (leaving mark after it)."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
165 (interactive (list (read-shell-command "Shell command: ")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
166 current-prefix-arg))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
167 (if (and output-buffer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
168 (not (or (bufferp output-buffer) (stringp output-buffer))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
169 (progn (barf-if-buffer-read-only)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
170 (push-mark)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
171 ;; We do not use -f for csh; we will not support broken use of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
172 ;; .cshrcs. Even the BSD csh manual says to use
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
173 ;; "if ($?prompt) exit" before things which are not useful
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
174 ;; non-interactively. Besides, if someone wants their other
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
175 ;; aliases for shell commands then they can still have them.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
176 (call-process shell-file-name nil t nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
177 shell-command-switch command)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
178 (exchange-point-and-mark t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
179 ;; Preserve the match data in case called from a program.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
180 (save-match-data
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
181 (if (string-match "[ \t]*&[ \t]*$" command)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
182 ;; Command ending with ampersand means asynchronous.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
183 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
184 (background (substring command 0 (match-beginning 0))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
185 (shell-command-on-region (point) (point) command output-buffer)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
186
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
187 ;; We have a sentinel to prevent insertion of a termination message
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
188 ;; in the buffer itself.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
189 (defun shell-command-sentinel (process signal)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
190 (if (memq (process-status process) '(exit signal))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
191 (message "%s: %s."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
192 (car (cdr (cdr (process-command process))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
193 (substring signal 0 -1))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
194
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
195 (defun shell-command-on-region (start end command
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
196 &optional output-buffer replace)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
197 "Execute string COMMAND in inferior shell with region as input.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
198 Normally display output (if any) in temp buffer `*Shell Command Output*';
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
199 Prefix arg means replace the region with it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
200
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
201 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, REPLACE.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
202 If REPLACE is non-nil, that means insert the output
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
203 in place of text from START to END, putting point and mark around it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
204
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
205 If the output is one line, it is displayed in the echo area,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
206 but it is nonetheless available in buffer `*Shell Command Output*'
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
207 even though that buffer is not automatically displayed.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
208 If there is no output, or if output is inserted in the current buffer,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
209 then `*Shell Command Output*' is deleted.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
210
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
211 If the optional fourth argument OUTPUT-BUFFER is non-nil,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
212 that says to put the output in some other buffer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
213 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
214 If OUTPUT-BUFFER is not a buffer and not nil,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
215 insert output in the current buffer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
216 In either case, the output is inserted after point (leaving mark after it)."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
217 (interactive (let ((string
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
218 ;; Do this before calling region-beginning
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
219 ;; and region-end, in case subprocess output
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
220 ;; relocates them while we are in the minibuffer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
221 (read-shell-command "Shell command on region: ")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
222 ;; call-interactively recognizes region-beginning and
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
223 ;; region-end specially, leaving them in the history.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
224 (list (region-beginning) (region-end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
225 string
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
226 current-prefix-arg
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
227 current-prefix-arg)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
228 (if (or replace
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
229 (and output-buffer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
230 (not (or (bufferp output-buffer) (stringp output-buffer)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
231 ;; Replace specified region with output from command.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
232 (let ((swap (and replace (< start end))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
233 ;; Don't muck with mark unless REPLACE says we should.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
234 (goto-char start)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
235 (and replace (push-mark))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
236 (call-process-region start end shell-file-name t t nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
237 shell-command-switch command)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
238 (let ((shell-buffer (get-buffer "*Shell Command Output*")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
239 (and shell-buffer (not (eq shell-buffer (current-buffer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
240 (kill-buffer shell-buffer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
241 ;; Don't muck with mark unless REPLACE says we should.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
242 (and replace swap (exchange-point-and-mark t)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
243 ;; No prefix argument: put the output in a temp buffer,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
244 ;; replacing its entire contents.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
245 (let ((buffer (get-buffer-create
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
246 (or output-buffer "*Shell Command Output*")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
247 (success nil)
377
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
248 (exit-status nil)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
249 (directory default-directory))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
250 (unwind-protect
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
251 (if (eq buffer (current-buffer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
252 ;; If the input is the same buffer as the output,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
253 ;; delete everything but the specified region,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
254 ;; then replace that region with the output.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
255 (progn (setq buffer-read-only nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
256 (delete-region (max start end) (point-max))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
257 (delete-region (point-min) (max start end))
377
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
258 (setq exit-status
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
259 (call-process-region (point-min) (point-max)
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
260 shell-file-name t t nil
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
261 shell-command-switch command))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
262 (setq success t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
263 ;; Clear the output buffer,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
264 ;; then run the command with output there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
265 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
266 (set-buffer buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
267 (setq buffer-read-only nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
268 ;; XEmacs change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
269 (setq default-directory directory)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
270 (erase-buffer))
377
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
271 (setq exit-status
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
272 (call-process-region start end shell-file-name
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
273 nil buffer nil
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
274 shell-command-switch command))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
275 (setq success t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
276 ;; Report the amount of output.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
277 (let ((lines (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
278 (set-buffer buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
279 (if (= (buffer-size) 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
280 0
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
281 (count-lines (point-min) (point-max))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
282 (cond ((= lines 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
283 (if success
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
284 (display-message
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
285 'command
377
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
286 (if (eql exit-status 0)
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
287 "(Shell command succeeded with no output)"
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
288 "(Shell command failed with no output)")))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
289 (kill-buffer buffer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
290 ((and success (= lines 1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
291 (message "%s"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
292 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
293 (set-buffer buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
294 (goto-char (point-min))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
295 (buffer-substring (point)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
296 (progn (end-of-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
297 (point))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
298 (t
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
299 (set-window-start (display-buffer buffer) 1))))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
300
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
301
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
302 (defun start-process (name buffer program &rest program-args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
303 "Start a program in a subprocess. Return the process object for it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
304 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
305 NAME is name for process. It is modified if necessary to make it unique.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
306 BUFFER is the buffer or (buffer-name) to associate with the process.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
307 Process output goes at end of that buffer, unless you specify
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
308 an output stream or filter function to handle the output.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
309 BUFFER may be also nil, meaning that this process is not associated
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
310 with any buffer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
311 Third arg is program file name. It is searched for as in the shell.
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents: 263
diff changeset
312 Remaining arguments are strings to give program as arguments."
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
313 (apply 'start-process-internal name buffer program program-args))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
314
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
315 (defun open-network-stream (name buffer host service)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
316 "Open a TCP connection for a service to a host.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
317 Returns a subprocess-object to represent the connection.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
318 Input and output work as for subprocesses; `delete-process' closes it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
319 Args are NAME BUFFER HOST SERVICE.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
320 NAME is name for process. It is modified if necessary to make it unique.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
321 BUFFER is the buffer (or buffer-name) to associate with the process.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
322 Process output goes at end of that buffer, unless you specify
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
323 an output stream or filter function to handle the output.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
324 BUFFER may be also nil, meaning that this process is not associated
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
325 with any buffer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
326 Third arg is name of the host to connect to, or its IP address.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
327 Fourth arg SERVICE is name of the service desired, or an integer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
328 specifying a port number to connect to."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
329 (open-network-stream-internal name buffer host service))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
330
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
331 (defun shell-quote-argument (argument)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
332 "Quote an argument for passing as argument to an inferior shell."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
333 (if (eq system-type 'ms-dos)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
334 ;; MS-DOS shells don't have quoting, so don't do any.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
335 argument
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
336 (if (eq system-type 'windows-nt)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
337 (concat "\"" argument "\"")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
338 ;; Quote everything except POSIX filename characters.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
339 ;; This should be safe enough even for really weird shells.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
340 (let ((result "") (start 0) end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
341 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
342 (setq end (match-beginning 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
343 result (concat result (substring argument start end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
344 "\\" (substring argument end (1+ end)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
345 start (1+ end)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
346 (concat result (substring argument start))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
347
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
348 (defun exec-to-string (command)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
349 "Execute COMMAND as an external process and return the output of that
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
350 process as a string"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
351 ;; by "William G. Dubuque" <wgd@zurich.ai.mit.edu>
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
352 (with-output-to-string
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 333
diff changeset
353 (call-process shell-file-name nil t nil "-c" command)))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
354
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
355 (defalias 'shell-command-to-string 'exec-to-string)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
356
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
357 ;;; process.el ends here