Mercurial > emacs
annotate lispref/processes.texi @ 25448:95f7147cb572
Include <stdlib.h> if available. Don't declare errno if it's a macro.
author | Andreas Schwab <schwab@suse.de> |
---|---|
date | Mon, 30 Aug 1999 15:07:30 +0000 |
parents | 7451b1458af1 |
children | 467b88fab665 |
rev | line source |
---|---|
6558 | 1 @c -*-texinfo-*- |
2 @c This is part of the GNU Emacs Lisp Reference Manual. | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc. |
6558 | 4 @c See the file elisp.texi for copying conditions. |
5 @setfilename ../info/processes | |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
6 @node Processes, Display, Abbrevs, Top |
6558 | 7 @chapter Processes |
8 @cindex child process | |
9 @cindex parent process | |
10 @cindex subprocess | |
11 @cindex process | |
12 | |
13 In the terminology of operating systems, a @dfn{process} is a space in | |
14 which a program can execute. Emacs runs in a process. Emacs Lisp | |
15 programs can invoke other programs in processes of their own. These are | |
16 called @dfn{subprocesses} or @dfn{child processes} of the Emacs process, | |
17 which is their @dfn{parent process}. | |
18 | |
19 A subprocess of Emacs may be @dfn{synchronous} or @dfn{asynchronous}, | |
20 depending on how it is created. When you create a synchronous | |
21 subprocess, the Lisp program waits for the subprocess to terminate | |
22 before continuing execution. When you create an asynchronous | |
23 subprocess, it can run in parallel with the Lisp program. This kind of | |
24 subprocess is represented within Emacs by a Lisp object which is also | |
25 called a ``process''. Lisp programs can use this object to communicate | |
26 with the subprocess or to control it. For example, you can send | |
27 signals, obtain status information, receive output from the process, or | |
28 send input to it. | |
29 | |
30 @defun processp object | |
31 This function returns @code{t} if @var{object} is a process, | |
32 @code{nil} otherwise. | |
33 @end defun | |
34 | |
35 @menu | |
36 * Subprocess Creation:: Functions that start subprocesses. | |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
37 * Shell Arguments:: Quoting an argument to pass it to a shell. |
6558 | 38 * Synchronous Processes:: Details of using synchronous subprocesses. |
39 * Asynchronous Processes:: Starting up an asynchronous subprocess. | |
40 * Deleting Processes:: Eliminating an asynchronous subprocess. | |
41 * Process Information:: Accessing run-status and other attributes. | |
42 * Input to Processes:: Sending input to an asynchronous subprocess. | |
43 * Signals to Processes:: Stopping, continuing or interrupting | |
44 an asynchronous subprocess. | |
45 * Output from Processes:: Collecting output from an asynchronous subprocess. | |
46 * Sentinels:: Sentinels run when process run-status changes. | |
47 * Transaction Queues:: Transaction-based communication with subprocesses. | |
12098 | 48 * Network:: Opening network connections. |
6558 | 49 @end menu |
50 | |
51 @node Subprocess Creation | |
52 @section Functions that Create Subprocesses | |
53 | |
54 There are three functions that create a new subprocess in which to run | |
55 a program. One of them, @code{start-process}, creates an asynchronous | |
56 process and returns a process object (@pxref{Asynchronous Processes}). | |
57 The other two, @code{call-process} and @code{call-process-region}, | |
58 create a synchronous process and do not return a process object | |
59 (@pxref{Synchronous Processes}). | |
60 | |
61 Synchronous and asynchronous processes are explained in following | |
62 sections. Since the three functions are all called in a similar | |
63 fashion, their common arguments are described here. | |
64 | |
65 @cindex execute program | |
66 @cindex @code{PATH} environment variable | |
67 @cindex @code{HOME} environment variable | |
68 In all cases, the function's @var{program} argument specifies the | |
69 program to be run. An error is signaled if the file is not found or | |
70 cannot be executed. If the file name is relative, the variable | |
71 @code{exec-path} contains a list of directories to search. Emacs | |
72 initializes @code{exec-path} when it starts up, based on the value of | |
73 the environment variable @code{PATH}. The standard file name | |
74 constructs, @samp{~}, @samp{.}, and @samp{..}, are interpreted as usual | |
75 in @code{exec-path}, but environment variable substitutions | |
76 (@samp{$HOME}, etc.) are not recognized; use | |
77 @code{substitute-in-file-name} to perform them (@pxref{File Name | |
78 Expansion}). | |
79 | |
80 Each of the subprocess-creating functions has a @var{buffer-or-name} | |
81 argument which specifies where the standard output from the program will | |
22252
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
82 go. It should be a buffer or a buffer name; if it is a buffer name, |
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
83 that will create the buffer if it does not already exist. It can also |
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
84 be @code{nil}, which says to discard the output unless a filter function |
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
85 handles it. (@xref{Filter Functions}, and @ref{Read and Print}.) |
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
86 Normally, you should avoid having multiple processes send output to the |
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
87 same buffer because their output would be intermixed randomly. |
6558 | 88 |
89 @cindex program arguments | |
90 All three of the subprocess-creating functions have a @code{&rest} | |
91 argument, @var{args}. The @var{args} must all be strings, and they are | |
92 supplied to @var{program} as separate command line arguments. Wildcard | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
93 characters and other shell constructs have no special meanings in these |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
94 strings, since the whole strings are passed directly to the specified |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
95 program. |
6558 | 96 |
7735
7db892210924
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
7411
diff
changeset
|
97 @strong{Please note:} The argument @var{program} contains only the |
6558 | 98 name of the program; it may not contain any command-line arguments. You |
99 must use @var{args} to provide those. | |
100 | |
101 The subprocess gets its current directory from the value of | |
102 @code{default-directory} (@pxref{File Name Expansion}). | |
103 | |
104 @cindex environment variables, subprocesses | |
22252
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
105 The subprocess inherits its environment from Emacs, but you can |
6558 | 106 specify overrides for it with @code{process-environment}. @xref{System |
107 Environment}. | |
108 | |
109 @defvar exec-directory | |
17611
1b2afa6391ca
(exec-directory): Wakeup no longer exists, so use movemail as example.
Richard M. Stallman <rms@gnu.org>
parents:
16664
diff
changeset
|
110 @pindex movemail |
6558 | 111 The value of this variable is the name of a directory (a string) that |
22252
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
112 contains programs that come with GNU Emacs, programs intended for Emacs |
17611
1b2afa6391ca
(exec-directory): Wakeup no longer exists, so use movemail as example.
Richard M. Stallman <rms@gnu.org>
parents:
16664
diff
changeset
|
113 to invoke. The program @code{movemail} is an example of such a program; |
1b2afa6391ca
(exec-directory): Wakeup no longer exists, so use movemail as example.
Richard M. Stallman <rms@gnu.org>
parents:
16664
diff
changeset
|
114 Rmail uses it to fetch new mail from an inbox. |
6558 | 115 @end defvar |
116 | |
117 @defopt exec-path | |
118 The value of this variable is a list of directories to search for | |
119 programs to run in subprocesses. Each element is either the name of a | |
120 directory (i.e., a string), or @code{nil}, which stands for the default | |
121 directory (which is the value of @code{default-directory}). | |
122 @cindex program directories | |
123 | |
124 The value of @code{exec-path} is used by @code{call-process} and | |
125 @code{start-process} when the @var{program} argument is not an absolute | |
126 file name. | |
127 @end defopt | |
128 | |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
129 @node Shell Arguments |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
130 @section Shell Arguments |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
131 |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
132 Lisp programs sometimes need to run a shell and give it a command |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
133 which contains file names that were specified by the user. These |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
134 programs ought to be able to support any valid file name. But the shell |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
135 gives special treatment to certain characters, and if these characters |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
136 occur in the file name, they will confuse the shell. To handle these |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
137 characters, use the function @code{shell-quote-argument}: |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
138 |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
139 @defun shell-quote-argument argument |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
140 This function returns a string which represents, in shell syntax, |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
141 an argument whose actual contents are @var{argument}. It should |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
142 work reliably to concatenate the return value into a shell command |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
143 and then pass it to a shell for execution. |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
144 |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
145 Precisely what this function does depends on your operating system. The |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
146 function is designed to work with the usual shell syntax; if you use an |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
147 unusual shell, you will need to redefine this function. On MS-DOS, the |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
148 function returns @var{argument} unchanged; while this is not really |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
149 correct, it is the best one can do, since the MS-DOS shell has no |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
150 quoting features. |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
151 |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
152 @example |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
153 ;; @r{This example shows the behavior on GNU and Unix systems.} |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
154 (shell-quote-argument "foo > bar") |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
155 @result{} "foo\\ \\>\\ bar" |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
156 @end example |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
157 |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
158 Here's an example of using @code{shell-quote-argument} to construct |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
159 a shell command: |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
160 |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
161 @example |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
162 (concat "diff -c " |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
163 (shell-quote-argument oldfile) |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
164 " " |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
165 (shell-quote-argument newfile)) |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
166 @end example |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
167 @end defun |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
168 |
6558 | 169 @node Synchronous Processes |
170 @section Creating a Synchronous Process | |
171 @cindex synchronous subprocess | |
172 | |
173 After a @dfn{synchronous process} is created, Emacs waits for the | |
174 process to terminate before continuing. Starting Dired is an example of | |
175 this: it runs @code{ls} in a synchronous process, then modifies the | |
176 output slightly. Because the process is synchronous, the entire | |
177 directory listing arrives in the buffer before Emacs tries to do | |
178 anything with it. | |
179 | |
180 While Emacs waits for the synchronous subprocess to terminate, the | |
181 user can quit by typing @kbd{C-g}. The first @kbd{C-g} tries to kill | |
182 the subprocess with a @code{SIGINT} signal; but it waits until the | |
183 subprocess actually terminates before quitting. If during that time the | |
184 user types another @kbd{C-g}, that kills the subprocess instantly with | |
185 @code{SIGKILL} and quits immediately. @xref{Quitting}. | |
186 | |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
187 The synchronous subprocess functions return an indication of how the |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
188 process terminated. |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
189 |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
190 The output from a synchronous subprocess is generally decoded using a |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
191 coding system, much like text read from a file. The input sent to a |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
192 subprocess by @code{call-process-region} is encoded using a coding |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
193 system, much like text written into a file. @xref{Coding Systems}. |
6558 | 194 |
12067 | 195 @defun call-process program &optional infile destination display &rest args |
6558 | 196 This function calls @var{program} in a separate process and waits for |
197 it to finish. | |
198 | |
199 The standard input for the process comes from file @var{infile} if | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
200 @var{infile} is not @code{nil}, and from @file{/dev/null} otherwise. |
12067 | 201 The argument @var{destination} says where to put the process output. |
202 Here are the possibilities: | |
203 | |
204 @table @asis | |
205 @item a buffer | |
206 Insert the output in that buffer, before point. This includes both the | |
207 standard output stream and the standard error stream of the process. | |
208 | |
209 @item a string | |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
210 Insert the output in a buffer with that name, before point. |
12067 | 211 |
212 @item @code{t} | |
213 Insert the output in the current buffer, before point. | |
214 | |
215 @item @code{nil} | |
216 Discard the output. | |
6558 | 217 |
12067 | 218 @item 0 |
219 Discard the output, and return immediately without waiting | |
220 for the subprocess to finish. | |
221 | |
222 In this case, the process is not truly synchronous, since it can run in | |
223 parallel with Emacs; but you can think of it as synchronous in that | |
224 Emacs is essentially finished with the subprocess as soon as this | |
225 function returns. | |
226 | |
22252
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
227 @item @code{(@var{real-destination} @var{error-destination})} |
12067 | 228 Keep the standard output stream separate from the standard error stream; |
229 deal with the ordinary output as specified by @var{real-destination}, | |
230 and dispose of the error output according to @var{error-destination}. | |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
231 If @var{error-destination} is @code{nil}, that means to discard the |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
232 error output, @code{t} means mix it with the ordinary output, and a |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
233 string specifies a file name to redirect error output into. |
12067 | 234 |
235 You can't directly specify a buffer to put the error output in; that is | |
236 too difficult to implement. But you can achieve this result by sending | |
237 the error output to a temporary file and then inserting the file into a | |
238 buffer. | |
239 @end table | |
6558 | 240 |
241 If @var{display} is non-@code{nil}, then @code{call-process} redisplays | |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
242 the buffer as output is inserted. (However, if the coding system chosen |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
243 for decoding output is @code{undecided}, meaning deduce the encoding |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
244 from the actual data, then redisplay sometimes cannot continue once |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
245 non-@sc{ASCII} characters are encountered. There are fundamental |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
246 reasons why it is hard to fix this.) Otherwise the function |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
247 @code{call-process} does no redisplay, and the results become visible on |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
248 the screen only when Emacs redisplays that buffer in the normal course |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
249 of events. |
6558 | 250 |
251 The remaining arguments, @var{args}, are strings that specify command | |
252 line arguments for the program. | |
253 | |
254 The value returned by @code{call-process} (unless you told it not to | |
255 wait) indicates the reason for process termination. A number gives the | |
256 exit status of the subprocess; 0 means success, and any other value | |
257 means failure. If the process terminated with a signal, | |
258 @code{call-process} returns a string describing the signal. | |
259 | |
260 In the examples below, the buffer @samp{foo} is current. | |
261 | |
262 @smallexample | |
263 @group | |
264 (call-process "pwd" nil t) | |
265 @result{} nil | |
266 | |
267 ---------- Buffer: foo ---------- | |
268 /usr/user/lewis/manual | |
269 ---------- Buffer: foo ---------- | |
270 @end group | |
271 | |
272 @group | |
273 (call-process "grep" nil "bar" nil "lewis" "/etc/passwd") | |
274 @result{} nil | |
275 | |
276 ---------- Buffer: bar ---------- | |
277 lewis:5LTsHm66CSWKg:398:21:Bil Lewis:/user/lewis:/bin/csh | |
278 | |
279 ---------- Buffer: bar ---------- | |
280 @end group | |
281 @end smallexample | |
282 | |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
283 Here is a good example of the use of @code{call-process}, which used to |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
284 be found in the definition of @code{insert-directory}: |
6558 | 285 |
286 @smallexample | |
287 @group | |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
288 (call-process insert-directory-program nil t nil @var{switches} |
6558 | 289 (if full-directory-p |
290 (concat (file-name-as-directory file) ".") | |
291 file)) | |
292 @end group | |
293 @end smallexample | |
294 @end defun | |
295 | |
12067 | 296 @defun call-process-region start end program &optional delete destination display &rest args |
6558 | 297 This function sends the text between @var{start} to @var{end} as |
298 standard input to a process running @var{program}. It deletes the text | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
299 sent if @var{delete} is non-@code{nil}; this is useful when |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
300 @var{destination} is @code{t}, to insert the output in the current |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
301 buffer in place of the input. |
6558 | 302 |
12067 | 303 The arguments @var{destination} and @var{display} control what to do |
6558 | 304 with the output from the subprocess, and whether to update the display |
305 as it comes in. For details, see the description of | |
12067 | 306 @code{call-process}, above. If @var{destination} is the integer 0, |
6558 | 307 @code{call-process-region} discards the output and returns @code{nil} |
308 immediately, without waiting for the subprocess to finish. | |
309 | |
310 The remaining arguments, @var{args}, are strings that specify command | |
311 line arguments for the program. | |
312 | |
313 The return value of @code{call-process-region} is just like that of | |
314 @code{call-process}: @code{nil} if you told it to return without | |
315 waiting; otherwise, a number or string which indicates how the | |
316 subprocess terminated. | |
317 | |
318 In the following example, we use @code{call-process-region} to run the | |
319 @code{cat} utility, with standard input being the first five characters | |
320 in buffer @samp{foo} (the word @samp{input}). @code{cat} copies its | |
321 standard input into its standard output. Since the argument | |
12067 | 322 @var{destination} is @code{t}, this output is inserted in the current |
6558 | 323 buffer. |
324 | |
325 @smallexample | |
326 @group | |
327 ---------- Buffer: foo ---------- | |
328 input@point{} | |
329 ---------- Buffer: foo ---------- | |
330 @end group | |
331 | |
332 @group | |
333 (call-process-region 1 6 "cat" nil t) | |
334 @result{} nil | |
335 | |
336 ---------- Buffer: foo ---------- | |
337 inputinput@point{} | |
338 ---------- Buffer: foo ---------- | |
339 @end group | |
340 @end smallexample | |
341 | |
342 The @code{shell-command-on-region} command uses | |
343 @code{call-process-region} like this: | |
344 | |
345 @smallexample | |
346 @group | |
347 (call-process-region | |
348 start end | |
349 shell-file-name ; @r{Name of program.} | |
350 nil ; @r{Do not delete region.} | |
351 buffer ; @r{Send output to @code{buffer}.} | |
352 nil ; @r{No redisplay during output.} | |
353 "-c" command) ; @r{Arguments for the shell.} | |
354 @end group | |
355 @end smallexample | |
356 @end defun | |
357 | |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
358 @defun shell-command-to-string command |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
359 @tindex shell-command-to-string |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
360 This function executes @var{command} (a string) as a shell command, |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
361 then returns the command's output as a string. |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
362 @end defun |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
363 |
6558 | 364 @node Asynchronous Processes |
365 @section Creating an Asynchronous Process | |
366 @cindex asynchronous subprocess | |
367 | |
22252
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
368 After an @dfn{asynchronous process} is created, Emacs and the subprocess |
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
369 both continue running immediately. The process thereafter runs |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
370 in parallel with Emacs, and the two can communicate with each other |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
371 using the functions described in following sections. However, |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
372 communication is only partially asynchronous: Emacs sends data to the |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
373 process only when certain functions are called, and Emacs accepts data |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
374 from the process only when Emacs is waiting for input or for a time |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
375 delay. |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
376 |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
377 Here we describe how to create an asynchronous process. |
6558 | 378 |
379 @defun start-process name buffer-or-name program &rest args | |
380 This function creates a new asynchronous subprocess and starts the | |
381 program @var{program} running in it. It returns a process object that | |
382 stands for the new subprocess in Lisp. The argument @var{name} | |
383 specifies the name for the process object; if a process with this name | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
384 already exists, then @var{name} is modified (by appending @samp{<1>}, |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
385 etc.) to be unique. The buffer @var{buffer-or-name} is the buffer to |
6558 | 386 associate with the process. |
387 | |
388 The remaining arguments, @var{args}, are strings that specify command | |
389 line arguments for the program. | |
390 | |
391 In the example below, the first process is started and runs (rather, | |
392 sleeps) for 100 seconds. Meanwhile, the second process is started, and | |
393 given the name @samp{my-process<1>} for the sake of uniqueness. It | |
394 inserts the directory listing at the end of the buffer @samp{foo}, | |
395 before the first process finishes. Then it finishes, and a message to | |
396 that effect is inserted in the buffer. Much later, the first process | |
397 finishes, and another message is inserted in the buffer for it. | |
398 | |
399 @smallexample | |
400 @group | |
401 (start-process "my-process" "foo" "sleep" "100") | |
402 @result{} #<process my-process> | |
403 @end group | |
404 | |
405 @group | |
406 (start-process "my-process" "foo" "ls" "-l" "/user/lewis/bin") | |
407 @result{} #<process my-process<1>> | |
408 | |
409 ---------- Buffer: foo ---------- | |
410 total 2 | |
411 lrwxrwxrwx 1 lewis 14 Jul 22 10:12 gnuemacs --> /emacs | |
412 -rwxrwxrwx 1 lewis 19 Jul 30 21:02 lemon | |
413 | |
414 Process my-process<1> finished | |
415 | |
416 Process my-process finished | |
417 ---------- Buffer: foo ---------- | |
418 @end group | |
419 @end smallexample | |
420 @end defun | |
421 | |
422 @defun start-process-shell-command name buffer-or-name command &rest command-args | |
423 This function is like @code{start-process} except that it uses a shell | |
424 to execute the specified command. The argument @var{command} is a shell | |
425 command name, and @var{command-args} are the arguments for the shell | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
426 command. The variable @code{shell-file-name} specifies which shell to |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
427 use. |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
428 |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
429 The point of running a program through the shell, rather than directly |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
430 with @code{start-process}, is so that you can employ shell features such |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
431 as wildcards in the arguments. It follows that if you include an |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
432 arbitrary user-specified filename in the command, you should quote it |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
433 with @code{shell-quote-argument} first, so that any special shell |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
434 characters in the file name do @emph{not} have their special shell |
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
435 meanings. @xref{Shell Arguments}. |
6558 | 436 @end defun |
437 | |
438 @defvar process-connection-type | |
439 @cindex pipes | |
440 @cindex @sc{pty}s | |
441 This variable controls the type of device used to communicate with | |
12098 | 442 asynchronous subprocesses. If it is non-@code{nil}, then @sc{pty}s are |
443 used, when available. Otherwise, pipes are used. | |
6558 | 444 |
445 @sc{pty}s are usually preferable for processes visible to the user, as | |
446 in Shell mode, because they allow job control (@kbd{C-c}, @kbd{C-z}, | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
447 etc.) to work between the process and its children, whereas pipes do |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
448 not. For subprocesses used for internal purposes by programs, it is |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
449 often better to use a pipe, because they are more efficient. In |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
450 addition, the total number of @sc{pty}s is limited on many systems and |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
451 it is good not to waste them. |
6558 | 452 |
453 The value @code{process-connection-type} is used when | |
454 @code{start-process} is called. So you can specify how to communicate | |
455 with one subprocess by binding the variable around the call to | |
456 @code{start-process}. | |
457 | |
458 @smallexample | |
459 @group | |
460 (let ((process-connection-type nil)) ; @r{Use a pipe.} | |
461 (start-process @dots{})) | |
462 @end group | |
463 @end smallexample | |
12067 | 464 |
465 To determine whether a given subprocess actually got a pipe or a | |
466 @sc{pty}, use the function @code{process-tty-name} (@pxref{Process | |
467 Information}). | |
6558 | 468 @end defvar |
469 | |
470 @node Deleting Processes | |
471 @section Deleting Processes | |
472 @cindex deleting processes | |
473 | |
474 @dfn{Deleting a process} disconnects Emacs immediately from the | |
475 subprocess, and removes it from the list of active processes. It sends | |
476 a signal to the subprocess to make the subprocess terminate, but this is | |
477 not guaranteed to happen immediately. The process object itself | |
15759
e74f36ff89e7
Explain how deletion of process doesn't affect process mark.
Richard M. Stallman <rms@gnu.org>
parents:
12125
diff
changeset
|
478 continues to exist as long as other Lisp objects point to it. The |
e74f36ff89e7
Explain how deletion of process doesn't affect process mark.
Richard M. Stallman <rms@gnu.org>
parents:
12125
diff
changeset
|
479 process mark continues to point to the same place as before (usually |
e74f36ff89e7
Explain how deletion of process doesn't affect process mark.
Richard M. Stallman <rms@gnu.org>
parents:
12125
diff
changeset
|
480 into a buffer where output from the process was being inserted). |
6558 | 481 |
482 You can delete a process explicitly at any time. Processes are | |
483 deleted automatically after they terminate, but not necessarily right | |
484 away. If you delete a terminated process explicitly before it is | |
485 deleted automatically, no harm results. | |
486 | |
22252
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
487 @defopt delete-exited-processes |
6558 | 488 This variable controls automatic deletion of processes that have |
489 terminated (due to calling @code{exit} or to a signal). If it is | |
490 @code{nil}, then they continue to exist until the user runs | |
491 @code{list-processes}. Otherwise, they are deleted immediately after | |
492 they exit. | |
22252
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
493 @end defopt |
6558 | 494 |
495 @defun delete-process name | |
496 This function deletes the process associated with @var{name}, killing it | |
497 with a @code{SIGHUP} signal. The argument @var{name} may be a process, | |
498 the name of a process, a buffer, or the name of a buffer. | |
499 | |
500 @smallexample | |
501 @group | |
502 (delete-process "*shell*") | |
503 @result{} nil | |
504 @end group | |
505 @end smallexample | |
506 @end defun | |
507 | |
22252
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
508 @defun process-kill-without-query process &optional do-query |
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
509 This function specifies whether Emacs should query the user if |
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
510 @var{process} is still running when Emacs is exited. If @var{do-query} |
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
511 is @code{nil}, the process will be deleted silently. |
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
512 Otherwise, Emacs will query about killing it. |
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
513 |
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
514 The value is @code{t} if the process was formerly set up to require |
22267
dfac7398266b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22252
diff
changeset
|
515 query, @code{nil} otherwise. A newly-created process always requires |
22252
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
516 query. |
6558 | 517 |
518 @smallexample | |
519 @group | |
520 (process-kill-without-query (get-process "shell")) | |
521 @result{} t | |
522 @end group | |
523 @end smallexample | |
524 @end defun | |
525 | |
526 @node Process Information | |
527 @section Process Information | |
528 | |
529 Several functions return information about processes. | |
530 @code{list-processes} is provided for interactive use. | |
531 | |
532 @deffn Command list-processes | |
533 This command displays a listing of all living processes. In addition, | |
534 it finally deletes any process whose status was @samp{Exited} or | |
535 @samp{Signaled}. It returns @code{nil}. | |
536 @end deffn | |
537 | |
538 @defun process-list | |
539 This function returns a list of all processes that have not been deleted. | |
540 | |
541 @smallexample | |
542 @group | |
543 (process-list) | |
544 @result{} (#<process display-time> #<process shell>) | |
545 @end group | |
546 @end smallexample | |
547 @end defun | |
548 | |
549 @defun get-process name | |
550 This function returns the process named @var{name}, or @code{nil} if | |
551 there is none. An error is signaled if @var{name} is not a string. | |
552 | |
553 @smallexample | |
554 @group | |
555 (get-process "shell") | |
556 @result{} #<process shell> | |
557 @end group | |
558 @end smallexample | |
559 @end defun | |
560 | |
561 @defun process-command process | |
562 This function returns the command that was executed to start | |
563 @var{process}. This is a list of strings, the first string being the | |
564 program executed and the rest of the strings being the arguments that | |
565 were given to the program. | |
566 | |
567 @smallexample | |
568 @group | |
569 (process-command (get-process "shell")) | |
570 @result{} ("/bin/csh" "-i") | |
571 @end group | |
572 @end smallexample | |
573 @end defun | |
574 | |
575 @defun process-id process | |
576 This function returns the @sc{pid} of @var{process}. This is an | |
9009 | 577 integer that distinguishes the process @var{process} from all other |
6558 | 578 processes running on the same computer at the current time. The |
579 @sc{pid} of a process is chosen by the operating system kernel when the | |
580 process is started and remains constant as long as the process exists. | |
581 @end defun | |
582 | |
583 @defun process-name process | |
584 This function returns the name of @var{process}. | |
585 @end defun | |
586 | |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
587 @defun process-contact process |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
588 @tindex process-contact |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
589 This function returns @code{t} for an ordinary child process, and |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
590 @code{(@var{hostname} @var{service})} for a net connection |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
591 (@pxref{Network}). |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
592 @end defun |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
593 |
6558 | 594 @defun process-status process-name |
595 This function returns the status of @var{process-name} as a symbol. | |
596 The argument @var{process-name} must be a process, a buffer, a | |
597 process name (string) or a buffer name (string). | |
598 | |
599 The possible values for an actual subprocess are: | |
600 | |
601 @table @code | |
602 @item run | |
603 for a process that is running. | |
604 @item stop | |
605 for a process that is stopped but continuable. | |
606 @item exit | |
607 for a process that has exited. | |
608 @item signal | |
609 for a process that has received a fatal signal. | |
610 @item open | |
611 for a network connection that is open. | |
612 @item closed | |
613 for a network connection that is closed. Once a connection | |
614 is closed, you cannot reopen it, though you might be able to open | |
615 a new connection to the same place. | |
616 @item nil | |
617 if @var{process-name} is not the name of an existing process. | |
618 @end table | |
619 | |
620 @smallexample | |
621 @group | |
622 (process-status "shell") | |
623 @result{} run | |
624 @end group | |
625 @group | |
626 (process-status (get-buffer "*shell*")) | |
627 @result{} run | |
628 @end group | |
629 @group | |
630 x | |
631 @result{} #<process xx<1>> | |
632 (process-status x) | |
633 @result{} exit | |
634 @end group | |
635 @end smallexample | |
636 | |
637 For a network connection, @code{process-status} returns one of the symbols | |
638 @code{open} or @code{closed}. The latter means that the other side | |
639 closed the connection, or Emacs did @code{delete-process}. | |
640 @end defun | |
641 | |
642 @defun process-exit-status process | |
643 This function returns the exit status of @var{process} or the signal | |
644 number that killed it. (Use the result of @code{process-status} to | |
645 determine which of those it is.) If @var{process} has not yet | |
646 terminated, the value is 0. | |
647 @end defun | |
648 | |
12067 | 649 @defun process-tty-name process |
650 This function returns the terminal name that @var{process} is using for | |
651 its communication with Emacs---or @code{nil} if it is using pipes | |
652 instead of a terminal (see @code{process-connection-type} in | |
653 @ref{Asynchronous Processes}). | |
654 @end defun | |
655 | |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
656 @defun process-coding-system process |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
657 @tindex process-coding-system |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
658 This function returns a cons cell describing the coding systems in use |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
659 for decoding output from @var{process} and for encoding input to |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
660 @var{process} (@pxref{Coding Systems}). The value has this form: |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
661 |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
662 @example |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
663 (@var{coding-system-for-decoding} . @var{coding-system-for-encoding}) |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
664 @end example |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
665 @end defun |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
666 |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
667 @defun set-process-coding-system process decoding-system encoding-system |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
668 @tindex set-process-coding-system |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
669 This function specifies the coding systems to use for subsequent output |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
670 from and input to @var{process}. It will use @var{decoding-system} to |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
671 decode subprocess output, and @var{encoding-system} to encode subprocess |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
672 input. |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
673 @end defun |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
674 |
6558 | 675 @node Input to Processes |
676 @section Sending Input to Processes | |
677 @cindex process input | |
678 | |
679 Asynchronous subprocesses receive input when it is sent to them by | |
680 Emacs, which is done with the functions in this section. You must | |
681 specify the process to send input to, and the input data to send. The | |
682 data appears on the ``standard input'' of the subprocess. | |
683 | |
684 Some operating systems have limited space for buffered input in a | |
685 @sc{pty}. On these systems, Emacs sends an @sc{eof} periodically amidst | |
686 the other characters, to force them through. For most programs, | |
687 these @sc{eof}s do no harm. | |
688 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
689 Subprocess input is normally encoded using a coding system before the |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
690 subprocess receives it, much like text written into a file. You can use |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
691 @code{set-process-coding-system} to specify which coding system to use |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
692 (@pxref{Process Information}). Otherwise, the coding system comes from |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
693 @code{coding-system-for-write}, if that is non-@code{nil}; or else from |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
694 the defaulting mechanism (@pxref{Default Coding Systems}). |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
695 |
24951
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
696 Sometimes the system is unable to accept input for that process, |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
697 because the input buffer is full. When this happens, the send functions |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
698 wait a short while, accepting output from subprocesses, and then try |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
699 again. This gives the subprocess a chance to read more of its pending |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
700 input and make space in the buffer. It also allows filters, sentinels |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
701 and timers to run---so take account of that in writing your code. |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
702 |
6558 | 703 @defun process-send-string process-name string |
704 This function sends @var{process-name} the contents of @var{string} as | |
705 standard input. The argument @var{process-name} must be a process or | |
706 the name of a process. If it is @code{nil}, the current buffer's | |
707 process is used. | |
708 | |
709 The function returns @code{nil}. | |
710 | |
711 @smallexample | |
712 @group | |
713 (process-send-string "shell<1>" "ls\n") | |
714 @result{} nil | |
715 @end group | |
716 | |
717 | |
718 @group | |
719 ---------- Buffer: *shell* ---------- | |
720 ... | |
721 introduction.texi syntax-tables.texi~ | |
722 introduction.texi~ text.texi | |
723 introduction.txt text.texi~ | |
724 ... | |
725 ---------- Buffer: *shell* ---------- | |
726 @end group | |
727 @end smallexample | |
728 @end defun | |
729 | |
730 @deffn Command process-send-region process-name start end | |
731 This function sends the text in the region defined by @var{start} and | |
732 @var{end} as standard input to @var{process-name}, which is a process or | |
733 a process name. (If it is @code{nil}, the current buffer's process is | |
734 used.) | |
735 | |
736 An error is signaled unless both @var{start} and @var{end} are | |
737 integers or markers that indicate positions in the current buffer. (It | |
738 is unimportant which number is larger.) | |
739 @end deffn | |
740 | |
741 @defun process-send-eof &optional process-name | |
742 This function makes @var{process-name} see an end-of-file in its | |
743 input. The @sc{eof} comes after any text already sent to it. | |
744 | |
745 If @var{process-name} is not supplied, or if it is @code{nil}, then | |
746 this function sends the @sc{eof} to the current buffer's process. An | |
747 error is signaled if the current buffer has no process. | |
748 | |
749 The function returns @var{process-name}. | |
750 | |
751 @smallexample | |
752 @group | |
753 (process-send-eof "shell") | |
754 @result{} "shell" | |
755 @end group | |
756 @end smallexample | |
757 @end defun | |
758 | |
24951
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
759 @defun process-running-child-p process |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
760 @tindex process-running-child-p process |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
761 This function will tell you whether a subprocess has given control of |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
762 its terminal to its own child process. The value is @code{t} if this is |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
763 true, or if Emacs cannot tell; it is @code{nil} if Emacs can be certain |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
764 that this is not so. |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
765 @end defun |
7451b1458af1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22267
diff
changeset
|
766 |
6558 | 767 @node Signals to Processes |
768 @section Sending Signals to Processes | |
769 @cindex process signals | |
770 @cindex sending signals | |
771 @cindex signals | |
772 | |
773 @dfn{Sending a signal} to a subprocess is a way of interrupting its | |
774 activities. There are several different signals, each with its own | |
775 meaning. The set of signals and their names is defined by the operating | |
776 system. For example, the signal @code{SIGINT} means that the user has | |
777 typed @kbd{C-c}, or that some analogous thing has happened. | |
778 | |
779 Each signal has a standard effect on the subprocess. Most signals | |
780 kill the subprocess, but some stop or resume execution instead. Most | |
781 signals can optionally be handled by programs; if the program handles | |
782 the signal, then we can say nothing in general about its effects. | |
783 | |
784 You can send signals explicitly by calling the functions in this | |
785 section. Emacs also sends signals automatically at certain times: | |
786 killing a buffer sends a @code{SIGHUP} signal to all its associated | |
787 processes; killing Emacs sends a @code{SIGHUP} signal to all remaining | |
788 processes. (@code{SIGHUP} is a signal that usually indicates that the | |
789 user hung up the phone.) | |
790 | |
791 Each of the signal-sending functions takes two optional arguments: | |
792 @var{process-name} and @var{current-group}. | |
793 | |
794 The argument @var{process-name} must be either a process, the name of | |
795 one, or @code{nil}. If it is @code{nil}, the process defaults to the | |
796 process associated with the current buffer. An error is signaled if | |
797 @var{process-name} does not identify a process. | |
798 | |
799 The argument @var{current-group} is a flag that makes a difference | |
800 when you are running a job-control shell as an Emacs subprocess. If it | |
801 is non-@code{nil}, then the signal is sent to the current process-group | |
9009 | 802 of the terminal that Emacs uses to communicate with the subprocess. If |
6558 | 803 the process is a job-control shell, this means the shell's current |
804 subjob. If it is @code{nil}, the signal is sent to the process group of | |
805 the immediate subprocess of Emacs. If the subprocess is a job-control | |
806 shell, this is the shell itself. | |
807 | |
808 The flag @var{current-group} has no effect when a pipe is used to | |
809 communicate with the subprocess, because the operating system does not | |
810 support the distinction in the case of pipes. For the same reason, | |
811 job-control shells won't work when a pipe is used. See | |
812 @code{process-connection-type} in @ref{Asynchronous Processes}. | |
813 | |
814 @defun interrupt-process &optional process-name current-group | |
815 This function interrupts the process @var{process-name} by sending the | |
816 signal @code{SIGINT}. Outside of Emacs, typing the ``interrupt | |
817 character'' (normally @kbd{C-c} on some systems, and @code{DEL} on | |
818 others) sends this signal. When the argument @var{current-group} is | |
819 non-@code{nil}, you can think of this function as ``typing @kbd{C-c}'' | |
820 on the terminal by which Emacs talks to the subprocess. | |
821 @end defun | |
822 | |
823 @defun kill-process &optional process-name current-group | |
824 This function kills the process @var{process-name} by sending the | |
825 signal @code{SIGKILL}. This signal kills the subprocess immediately, | |
826 and cannot be handled by the subprocess. | |
827 @end defun | |
828 | |
829 @defun quit-process &optional process-name current-group | |
830 This function sends the signal @code{SIGQUIT} to the process | |
831 @var{process-name}. This signal is the one sent by the ``quit | |
832 character'' (usually @kbd{C-b} or @kbd{C-\}) when you are not inside | |
833 Emacs. | |
834 @end defun | |
835 | |
836 @defun stop-process &optional process-name current-group | |
837 This function stops the process @var{process-name} by sending the | |
838 signal @code{SIGTSTP}. Use @code{continue-process} to resume its | |
839 execution. | |
840 | |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
841 Outside of Emacs, on systems with job control, the ``stop character'' |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
842 (usually @kbd{C-z}) normally sends this signal. When |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
843 @var{current-group} is non-@code{nil}, you can think of this function as |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
844 ``typing @kbd{C-z}'' on the terminal Emacs uses to communicate with the |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
845 subprocess. |
6558 | 846 @end defun |
847 | |
848 @defun continue-process &optional process-name current-group | |
849 This function resumes execution of the process @var{process} by sending | |
850 it the signal @code{SIGCONT}. This presumes that @var{process-name} was | |
851 stopped previously. | |
852 @end defun | |
853 | |
854 @c Emacs 19 feature | |
855 @defun signal-process pid signal | |
856 This function sends a signal to process @var{pid}, which need not be | |
857 a child of Emacs. The argument @var{signal} specifies which signal | |
858 to send; it should be an integer. | |
859 @end defun | |
860 | |
861 @node Output from Processes | |
862 @section Receiving Output from Processes | |
863 @cindex process output | |
864 @cindex output from processes | |
865 | |
866 There are two ways to receive the output that a subprocess writes to | |
867 its standard output stream. The output can be inserted in a buffer, | |
868 which is called the associated buffer of the process, or a function | |
9009 | 869 called the @dfn{filter function} can be called to act on the output. If |
870 the process has no buffer and no filter function, its output is | |
871 discarded. | |
6558 | 872 |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
873 Output from a subprocess can arrive only while Emacs is waiting: when |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
874 reading terminal input, in @code{sit-for} and @code{sleep-for} |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
875 (@pxref{Waiting}), and in @code{accept-process-output} (@pxref{Accepting |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
876 Output}). This minimizes the problem of timing errors that usually |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
877 plague parallel programming. For example, you can safely create a |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
878 process and only then specify its buffer or filter function; no output |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
879 can arrive before you finish, if the code in between does not call any |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
880 primitive that waits. |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
881 |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
882 Subprocess output is normally decoded using a coding system before the |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
883 buffer or filter function receives it, much like text read from a file. |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
884 You can use @code{set-process-coding-system} to specify which coding |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
885 system to use (@pxref{Process Information}). Otherwise, the coding |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
886 system comes from @code{coding-system-for-read}, if that is |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
887 non-@code{nil}; or else from the defaulting mechanism (@pxref{Default |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
888 Coding Systems}). |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
889 |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
890 @strong{Warning:} Coding systems such as @code{undecided} which |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
891 determine the coding system from the data do not work entirely reliably |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
892 with asynchronous subprocess output. This is because Emacs has to |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
893 process asynchronous subprocess output in batches, as it arrives. Emacs |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
894 must try to detect the proper coding system from one batch at a time, |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
895 and this does not always work. Therefore, if at all possible, use a |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
896 coding system which determines both the character code conversion and |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
897 the end of line conversion---that is, one like @code{latin-1-unix}, |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
898 rather than @code{undecided} or @code{latin-1}. |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
899 |
6558 | 900 @menu |
901 * Process Buffers:: If no filter, output is put in a buffer. | |
902 * Filter Functions:: Filter functions accept output from the process. | |
903 * Accepting Output:: Explicitly permitting subprocess output. | |
904 Waiting for subprocess output. | |
905 @end menu | |
906 | |
907 @node Process Buffers | |
908 @subsection Process Buffers | |
909 | |
910 A process can (and usually does) have an @dfn{associated buffer}, | |
911 which is an ordinary Emacs buffer that is used for two purposes: storing | |
912 the output from the process, and deciding when to kill the process. You | |
913 can also use the buffer to identify a process to operate on, since in | |
914 normal practice only one process is associated with any given buffer. | |
915 Many applications of processes also use the buffer for editing input to | |
916 be sent to the process, but this is not built into Emacs Lisp. | |
917 | |
918 Unless the process has a filter function (@pxref{Filter Functions}), | |
919 its output is inserted in the associated buffer. The position to insert | |
9009 | 920 the output is determined by the @code{process-mark}, which is then |
921 updated to point to the end of the text just inserted. Usually, but not | |
922 always, the @code{process-mark} is at the end of the buffer. | |
6558 | 923 |
924 @defun process-buffer process | |
925 This function returns the associated buffer of the process | |
926 @var{process}. | |
927 | |
928 @smallexample | |
929 @group | |
930 (process-buffer (get-process "shell")) | |
931 @result{} #<buffer *shell*> | |
932 @end group | |
933 @end smallexample | |
934 @end defun | |
935 | |
936 @defun process-mark process | |
937 This function returns the process marker for @var{process}, which is the | |
938 marker that says where to insert output from the process. | |
939 | |
940 If @var{process} does not have a buffer, @code{process-mark} returns a | |
941 marker that points nowhere. | |
942 | |
943 Insertion of process output in a buffer uses this marker to decide where | |
944 to insert, and updates it to point after the inserted text. That is why | |
945 successive batches of output are inserted consecutively. | |
946 | |
947 Filter functions normally should use this marker in the same fashion | |
948 as is done by direct insertion of output in the buffer. A good | |
949 example of a filter function that uses @code{process-mark} is found at | |
950 the end of the following section. | |
951 | |
952 When the user is expected to enter input in the process buffer for | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
953 transmission to the process, the process marker separates the new input |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
954 from previous output. |
6558 | 955 @end defun |
956 | |
957 @defun set-process-buffer process buffer | |
958 This function sets the buffer associated with @var{process} to | |
959 @var{buffer}. If @var{buffer} is @code{nil}, the process becomes | |
960 associated with no buffer. | |
961 @end defun | |
962 | |
963 @defun get-buffer-process buffer-or-name | |
964 This function returns the process associated with @var{buffer-or-name}. | |
965 If there are several processes associated with it, then one is chosen. | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
966 (Currently, the one chosen is the one most recently created.) It is |
6558 | 967 usually a bad idea to have more than one process associated with the |
968 same buffer. | |
969 | |
970 @smallexample | |
971 @group | |
972 (get-buffer-process "*shell*") | |
973 @result{} #<process shell> | |
974 @end group | |
975 @end smallexample | |
976 | |
977 Killing the process's buffer deletes the process, which kills the | |
978 subprocess with a @code{SIGHUP} signal (@pxref{Signals to Processes}). | |
979 @end defun | |
980 | |
981 @node Filter Functions | |
982 @subsection Process Filter Functions | |
983 @cindex filter function | |
984 @cindex process filter | |
985 | |
986 A process @dfn{filter function} is a function that receives the | |
987 standard output from the associated process. If a process has a filter, | |
9009 | 988 then @emph{all} output from that process is passed to the filter. The |
989 process buffer is used directly for output from the process only when | |
990 there is no filter. | |
6558 | 991 |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
992 The filter function can only be called when Emacs is waiting for |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
993 something, because process output arrives only at such times. Emacs |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
994 waits when reading terminal input, in @code{sit-for} and |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
995 @code{sleep-for} (@pxref{Waiting}), and in @code{accept-process-output} |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
996 (@pxref{Accepting Output}). |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
997 |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
998 A filter function must accept two arguments: the associated process |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
999 and a string, which is output just received from it. The function is |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1000 then free to do whatever it chooses with the output. |
6558 | 1001 |
1002 Quitting is normally inhibited within a filter function---otherwise, | |
1003 the effect of typing @kbd{C-g} at command level or to quit a user | |
1004 command would be unpredictable. If you want to permit quitting inside a | |
1005 filter function, bind @code{inhibit-quit} to @code{nil}. | |
1006 @xref{Quitting}. | |
1007 | |
12067 | 1008 If an error happens during execution of a filter function, it is |
1009 caught automatically, so that it doesn't stop the execution of whatever | |
12125
995be67f3fd1
updates for version 19.29.
Melissa Weisshaus <melissa@gnu.org>
parents:
12098
diff
changeset
|
1010 program was running when the filter function was started. However, if |
12067 | 1011 @code{debug-on-error} is non-@code{nil}, the error-catching is turned |
1012 off. This makes it possible to use the Lisp debugger to debug the | |
1013 filter function. @xref{Debugger}. | |
1014 | |
6558 | 1015 Many filter functions sometimes or always insert the text in the |
1016 process's buffer, mimicking the actions of Emacs when there is no | |
1017 filter. Such filter functions need to use @code{set-buffer} in order to | |
1018 be sure to insert in that buffer. To avoid setting the current buffer | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1019 semipermanently, these filter functions must save and restore the |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1020 current buffer. They should also update the process marker, and in some |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1021 cases update the value of point. Here is how to do these things: |
6558 | 1022 |
1023 @smallexample | |
1024 @group | |
1025 (defun ordinary-insertion-filter (proc string) | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1026 (with-current-buffer (process-buffer proc) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1027 (let ((moving (= (point) (process-mark proc)))) |
6558 | 1028 @end group |
1029 @group | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1030 (save-excursion |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1031 ;; @r{Insert the text, advancing the process marker.} |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1032 (goto-char (process-mark proc)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1033 (insert string) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1034 (set-marker (process-mark proc) (point))) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1035 (if moving (goto-char (process-mark proc)))))) |
6558 | 1036 @end group |
1037 @end smallexample | |
1038 | |
1039 @noindent | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1040 The reason to use @code{with-current-buffer}, rather than using |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1041 @code{save-excursion} to save and restore the current buffer, is so as |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1042 to preserve the change in point made by the second call to |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1043 @code{goto-char}. |
6558 | 1044 |
1045 To make the filter force the process buffer to be visible whenever new | |
1046 text arrives, insert the following line just before the | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1047 @code{with-current-buffer} construct: |
6558 | 1048 |
1049 @smallexample | |
1050 (display-buffer (process-buffer proc)) | |
1051 @end smallexample | |
1052 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1053 To force point to the end of the new output, no matter where it was |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1054 previously, eliminate the variable @code{moving} and call |
6558 | 1055 @code{goto-char} unconditionally. |
1056 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1057 In earlier Emacs versions, every filter function that did regular |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1058 expression searching or matching had to explicitly save and restore the |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1059 match data. Now Emacs does this automatically for filter functions; |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1060 they never need to do it explicitly. @xref{Match Data}. |
6558 | 1061 |
1062 A filter function that writes the output into the buffer of the | |
12098 | 1063 process should check whether the buffer is still alive. If it tries to |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1064 insert into a dead buffer, it will get an error. The expression |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1065 @code{(buffer-name (process-buffer @var{process}))} returns @code{nil} |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1066 if the buffer is dead. |
6558 | 1067 |
1068 The output to the function may come in chunks of any size. A program | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1069 that produces the same output twice in a row may send it as one batch of |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1070 200 characters one time, and five batches of 40 characters the next. If |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1071 the filter looks for certain text strings in the subprocess output, make |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1072 sure to handle the case where one of these strings is split across two |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1073 or more batches of output. |
6558 | 1074 |
1075 @defun set-process-filter process filter | |
1076 This function gives @var{process} the filter function @var{filter}. If | |
1077 @var{filter} is @code{nil}, it gives the process no filter. | |
1078 @end defun | |
1079 | |
1080 @defun process-filter process | |
1081 This function returns the filter function of @var{process}, or @code{nil} | |
1082 if it has none. | |
1083 @end defun | |
1084 | |
1085 Here is an example of use of a filter function: | |
1086 | |
1087 @smallexample | |
1088 @group | |
1089 (defun keep-output (process output) | |
1090 (setq kept (cons output kept))) | |
1091 @result{} keep-output | |
1092 @end group | |
1093 @group | |
1094 (setq kept nil) | |
1095 @result{} nil | |
1096 @end group | |
1097 @group | |
1098 (set-process-filter (get-process "shell") 'keep-output) | |
1099 @result{} keep-output | |
1100 @end group | |
1101 @group | |
1102 (process-send-string "shell" "ls ~/other\n") | |
1103 @result{} nil | |
1104 kept | |
1105 @result{} ("lewis@@slug[8] % " | |
1106 @end group | |
1107 @group | |
1108 "FINAL-W87-SHORT.MSS backup.otl kolstad.mss~ | |
1109 address.txt backup.psf kolstad.psf | |
1110 backup.bib~ david.mss resume-Dec-86.mss~ | |
1111 backup.err david.psf resume-Dec.psf | |
1112 backup.mss dland syllabus.mss | |
1113 " | |
1114 "#backups.mss# backup.mss~ kolstad.mss | |
1115 ") | |
1116 @end group | |
1117 @end smallexample | |
1118 | |
1119 @ignore @c The code in this example doesn't show the right way to do things. | |
1120 Here is another, more realistic example, which demonstrates how to use | |
1121 the process mark to do insertion in the same fashion as is done when | |
1122 there is no filter function: | |
1123 | |
1124 @smallexample | |
1125 @group | |
1126 ;; @r{Insert input in the buffer specified by @code{my-shell-buffer}} | |
1127 ;; @r{and make sure that buffer is shown in some window.} | |
1128 (defun my-process-filter (proc str) | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1129 (let ((cur (selected-window)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1130 (pop-up-windows t)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1131 (pop-to-buffer my-shell-buffer) |
6558 | 1132 @end group |
1133 @group | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1134 (goto-char (point-max)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1135 (insert str) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1136 (set-marker (process-mark proc) (point-max)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1137 (select-window cur))) |
6558 | 1138 @end group |
1139 @end smallexample | |
1140 @end ignore | |
1141 | |
1142 @node Accepting Output | |
1143 @subsection Accepting Output from Processes | |
1144 | |
1145 Output from asynchronous subprocesses normally arrives only while | |
1146 Emacs is waiting for some sort of external event, such as elapsed time | |
1147 or terminal input. Occasionally it is useful in a Lisp program to | |
1148 explicitly permit output to arrive at a specific point, or even to wait | |
1149 until output arrives from a process. | |
1150 | |
1151 @defun accept-process-output &optional process seconds millisec | |
1152 This function allows Emacs to read pending output from processes. The | |
1153 output is inserted in the associated buffers or given to their filter | |
1154 functions. If @var{process} is non-@code{nil} then this function does | |
1155 not return until some output has been received from @var{process}. | |
1156 | |
1157 @c Emacs 19 feature | |
1158 The arguments @var{seconds} and @var{millisec} let you specify timeout | |
1159 periods. The former specifies a period measured in seconds and the | |
1160 latter specifies one measured in milliseconds. The two time periods | |
1161 thus specified are added together, and @code{accept-process-output} | |
1162 returns after that much time whether or not there has been any | |
1163 subprocess output. | |
1164 | |
12098 | 1165 The argument @var{seconds} need not be an integer. If it is a floating |
1166 point number, this function waits for a fractional number of seconds. | |
1167 Some systems support only a whole number of seconds; on these systems, | |
22252
40089afa2b1d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
22138
diff
changeset
|
1168 @var{seconds} is rounded down. |
12098 | 1169 |
6558 | 1170 Not all operating systems support waiting periods other than multiples |
1171 of a second; on those that do not, you get an error if you specify | |
1172 nonzero @var{millisec}. | |
1173 | |
1174 The function @code{accept-process-output} returns non-@code{nil} if it | |
1175 did get some output, or @code{nil} if the timeout expired before output | |
1176 arrived. | |
1177 @end defun | |
1178 | |
1179 @node Sentinels | |
1180 @section Sentinels: Detecting Process Status Changes | |
1181 @cindex process sentinel | |
1182 @cindex sentinel | |
1183 | |
1184 A @dfn{process sentinel} is a function that is called whenever the | |
1185 associated process changes status for any reason, including signals | |
1186 (whether sent by Emacs or caused by the process's own actions) that | |
1187 terminate, stop, or continue the process. The process sentinel is also | |
1188 called if the process exits. The sentinel receives two arguments: the | |
1189 process for which the event occurred, and a string describing the type | |
1190 of event. | |
1191 | |
1192 The string describing the event looks like one of the following: | |
1193 | |
1194 @itemize @bullet | |
1195 @item | |
1196 @code{"finished\n"}. | |
1197 | |
1198 @item | |
1199 @code{"exited abnormally with code @var{exitcode}\n"}. | |
1200 | |
1201 @item | |
1202 @code{"@var{name-of-signal}\n"}. | |
1203 | |
1204 @item | |
1205 @code{"@var{name-of-signal} (core dumped)\n"}. | |
1206 @end itemize | |
1207 | |
1208 A sentinel runs only while Emacs is waiting (e.g., for terminal input, | |
1209 or for time to elapse, or for process output). This avoids the timing | |
1210 errors that could result from running them at random places in the | |
1211 middle of other Lisp programs. A program can wait, so that sentinels | |
9009 | 1212 will run, by calling @code{sit-for} or @code{sleep-for} |
1213 (@pxref{Waiting}), or @code{accept-process-output} (@pxref{Accepting | |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
1214 Output}). Emacs also allows sentinels to run when the command loop is |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
1215 reading input. |
6558 | 1216 |
1217 Quitting is normally inhibited within a sentinel---otherwise, the | |
1218 effect of typing @kbd{C-g} at command level or to quit a user command | |
1219 would be unpredictable. If you want to permit quitting inside a | |
1220 sentinel, bind @code{inhibit-quit} to @code{nil}. @xref{Quitting}. | |
1221 | |
1222 A sentinel that writes the output into the buffer of the process | |
12098 | 1223 should check whether the buffer is still alive. If it tries to insert |
6558 | 1224 into a dead buffer, it will get an error. If the buffer is dead, |
1225 @code{(buffer-name (process-buffer @var{process}))} returns @code{nil}. | |
1226 | |
12067 | 1227 If an error happens during execution of a sentinel, it is caught |
1228 automatically, so that it doesn't stop the execution of whatever | |
1229 programs was running when the sentinel was started. However, if | |
1230 @code{debug-on-error} is non-@code{nil}, the error-catching is turned | |
1231 off. This makes it possible to use the Lisp debugger to debug the | |
1232 sentinel. @xref{Debugger}. | |
1233 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1234 In earlier Emacs versions, every sentinel that did regular expression |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1235 searching or matching had to explicitly save and restore the match data. |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1236 Now Emacs does this automatically for sentinels; they never need to do |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1237 it explicitly. @xref{Match Data}. |
12098 | 1238 |
6558 | 1239 @defun set-process-sentinel process sentinel |
1240 This function associates @var{sentinel} with @var{process}. If | |
1241 @var{sentinel} is @code{nil}, then the process will have no sentinel. | |
1242 The default behavior when there is no sentinel is to insert a message in | |
1243 the process's buffer when the process status changes. | |
1244 | |
1245 @smallexample | |
1246 @group | |
1247 (defun msg-me (process event) | |
1248 (princ | |
1249 (format "Process: %s had the event `%s'" process event))) | |
1250 (set-process-sentinel (get-process "shell") 'msg-me) | |
1251 @result{} msg-me | |
1252 @end group | |
1253 @group | |
1254 (kill-process (get-process "shell")) | |
1255 @print{} Process: #<process shell> had the event `killed' | |
1256 @result{} #<process shell> | |
1257 @end group | |
1258 @end smallexample | |
1259 @end defun | |
1260 | |
1261 @defun process-sentinel process | |
1262 This function returns the sentinel of @var{process}, or @code{nil} if it | |
1263 has none. | |
1264 @end defun | |
1265 | |
1266 @defun waiting-for-user-input-p | |
1267 While a sentinel or filter function is running, this function returns | |
1268 non-@code{nil} if Emacs was waiting for keyboard input from the user at | |
1269 the time the sentinel or filter function was called, @code{nil} if it | |
1270 was not. | |
1271 @end defun | |
1272 | |
1273 @node Transaction Queues | |
1274 @section Transaction Queues | |
1275 @cindex transaction queue | |
1276 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1277 You can use a @dfn{transaction queue} to communicate with a subprocess |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1278 using transactions. First use @code{tq-create} to create a transaction |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1279 queue communicating with a specified process. Then you can call |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1280 @code{tq-enqueue} to send a transaction. |
6558 | 1281 |
1282 @defun tq-create process | |
1283 This function creates and returns a transaction queue communicating with | |
1284 @var{process}. The argument @var{process} should be a subprocess | |
1285 capable of sending and receiving streams of bytes. It may be a child | |
9009 | 1286 process, or it may be a TCP connection to a server, possibly on another |
6558 | 1287 machine. |
1288 @end defun | |
1289 | |
1290 @defun tq-enqueue queue question regexp closure fn | |
1291 This function sends a transaction to queue @var{queue}. Specifying the | |
1292 queue has the effect of specifying the subprocess to talk to. | |
1293 | |
9009 | 1294 The argument @var{question} is the outgoing message that starts the |
6558 | 1295 transaction. The argument @var{fn} is the function to call when the |
1296 corresponding answer comes back; it is called with two arguments: | |
1297 @var{closure}, and the answer received. | |
1298 | |
1299 The argument @var{regexp} is a regular expression that should match the | |
1300 entire answer, but nothing less; that's how @code{tq-enqueue} determines | |
1301 where the answer ends. | |
1302 | |
1303 The return value of @code{tq-enqueue} itself is not meaningful. | |
1304 @end defun | |
1305 | |
1306 @defun tq-close queue | |
1307 Shut down transaction queue @var{queue}, waiting for all pending transactions | |
1308 to complete, and then terminate the connection or child process. | |
1309 @end defun | |
1310 | |
1311 Transaction queues are implemented by means of a filter function. | |
1312 @xref{Filter Functions}. | |
1313 | |
12098 | 1314 @node Network |
1315 @section Network Connections | |
1316 @cindex network connection | |
6558 | 1317 @cindex TCP |
1318 | |
12098 | 1319 Emacs Lisp programs can open TCP network connections to other processes on |
1320 the same machine or other machines. A network connection is handled by Lisp | |
6558 | 1321 much like a subprocess, and is represented by a process object. |
1322 However, the process you are communicating with is not a child of the | |
1323 Emacs process, so you can't kill it or send it signals. All you can do | |
1324 is send and receive data. @code{delete-process} closes the connection, | |
1325 but does not kill the process at the other end; that process must decide | |
1326 what to do about closure of the connection. | |
1327 | |
1328 You can distinguish process objects representing network connections | |
1329 from those representing subprocesses with the @code{process-status} | |
12098 | 1330 function. It always returns either @code{open} or @code{closed} for a |
1331 network connection, and it never returns either of those values for a | |
1332 real subprocess. @xref{Process Information}. | |
6558 | 1333 |
1334 @defun open-network-stream name buffer-or-name host service | |
1335 This function opens a TCP connection for a service to a host. It | |
1336 returns a process object to represent the connection. | |
1337 | |
1338 The @var{name} argument specifies the name for the process object. It | |
1339 is modified as necessary to make it unique. | |
1340 | |
1341 The @var{buffer-or-name} argument is the buffer to associate with the | |
1342 connection. Output from the connection is inserted in the buffer, | |
1343 unless you specify a filter function to handle the output. If | |
1344 @var{buffer-or-name} is @code{nil}, it means that the connection is not | |
1345 associated with any buffer. | |
1346 | |
1347 The arguments @var{host} and @var{service} specify where to connect to; | |
1348 @var{host} is the host name (a string), and @var{service} is the name of | |
1349 a defined network service (a string) or a port number (an integer). | |
1350 @end defun |