annotate lisp/prim/process.el @ 48:56c54cf7c5b6 r19-16b90

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