annotate lisp/process.el @ 380:8626e4521993 r21-2-5

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