annotate lisp/prim/process.el @ 7:c153ca296910

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