annotate lisp/prim/process.el @ 179:9ad43877534d r20-3b16

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