Mercurial > emacs
annotate lispref/processes.texi @ 22198:74d1c031f824
(fill-region-as-paragraph):
Don't add a newline at the end, when there is none.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 23 May 1998 20:00:18 +0000 |
parents | d4ac295a98b3 |
children | 40089afa2b1d |
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 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
82 go. It should be a buffer or a buffer name (which will create the |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
83 buffer if it does not already exist). It can also be @code{nil}, which |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
84 says to discard the output unless a filter function handles it. |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
85 (@xref{Filter Functions}, and @ref{Read and Print}.) Normally, you |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
86 should avoid having multiple processes send output to the same buffer |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
87 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 | |
105 The subprocess inherits its environment from Emacs; but you can | |
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 |
112 contains programs that come with GNU Emacs, that are 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 | |
227 @item (@var{real-destination} @var{error-destination}) | |
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 | |
368 After an @dfn{asynchronous process} is created, Emacs and the Lisp | |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
369 program both continue running immediately. The process thereafter runs |
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 | |
487 @defvar delete-exited-processes | |
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. | |
493 @end defvar | |
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 | |
508 @defun process-kill-without-query process | |
509 This function declares that Emacs need not query the user if | |
510 @var{process} is still running when Emacs is exited. The process will | |
511 be deleted silently. The value is @code{t}. | |
512 | |
513 @smallexample | |
514 @group | |
515 (process-kill-without-query (get-process "shell")) | |
516 @result{} t | |
517 @end group | |
518 @end smallexample | |
519 @end defun | |
520 | |
521 @node Process Information | |
522 @section Process Information | |
523 | |
524 Several functions return information about processes. | |
525 @code{list-processes} is provided for interactive use. | |
526 | |
527 @deffn Command list-processes | |
528 This command displays a listing of all living processes. In addition, | |
529 it finally deletes any process whose status was @samp{Exited} or | |
530 @samp{Signaled}. It returns @code{nil}. | |
531 @end deffn | |
532 | |
533 @defun process-list | |
534 This function returns a list of all processes that have not been deleted. | |
535 | |
536 @smallexample | |
537 @group | |
538 (process-list) | |
539 @result{} (#<process display-time> #<process shell>) | |
540 @end group | |
541 @end smallexample | |
542 @end defun | |
543 | |
544 @defun get-process name | |
545 This function returns the process named @var{name}, or @code{nil} if | |
546 there is none. An error is signaled if @var{name} is not a string. | |
547 | |
548 @smallexample | |
549 @group | |
550 (get-process "shell") | |
551 @result{} #<process shell> | |
552 @end group | |
553 @end smallexample | |
554 @end defun | |
555 | |
556 @defun process-command process | |
557 This function returns the command that was executed to start | |
558 @var{process}. This is a list of strings, the first string being the | |
559 program executed and the rest of the strings being the arguments that | |
560 were given to the program. | |
561 | |
562 @smallexample | |
563 @group | |
564 (process-command (get-process "shell")) | |
565 @result{} ("/bin/csh" "-i") | |
566 @end group | |
567 @end smallexample | |
568 @end defun | |
569 | |
570 @defun process-id process | |
571 This function returns the @sc{pid} of @var{process}. This is an | |
9009 | 572 integer that distinguishes the process @var{process} from all other |
6558 | 573 processes running on the same computer at the current time. The |
574 @sc{pid} of a process is chosen by the operating system kernel when the | |
575 process is started and remains constant as long as the process exists. | |
576 @end defun | |
577 | |
578 @defun process-name process | |
579 This function returns the name of @var{process}. | |
580 @end defun | |
581 | |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
582 @defun process-contact process |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
583 @tindex process-contact |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
584 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
|
585 @code{(@var{hostname} @var{service})} for a net connection |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
586 (@pxref{Network}). |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
587 @end defun |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
588 |
6558 | 589 @defun process-status process-name |
590 This function returns the status of @var{process-name} as a symbol. | |
591 The argument @var{process-name} must be a process, a buffer, a | |
592 process name (string) or a buffer name (string). | |
593 | |
594 The possible values for an actual subprocess are: | |
595 | |
596 @table @code | |
597 @item run | |
598 for a process that is running. | |
599 @item stop | |
600 for a process that is stopped but continuable. | |
601 @item exit | |
602 for a process that has exited. | |
603 @item signal | |
604 for a process that has received a fatal signal. | |
605 @item open | |
606 for a network connection that is open. | |
607 @item closed | |
608 for a network connection that is closed. Once a connection | |
609 is closed, you cannot reopen it, though you might be able to open | |
610 a new connection to the same place. | |
611 @item nil | |
612 if @var{process-name} is not the name of an existing process. | |
613 @end table | |
614 | |
615 @smallexample | |
616 @group | |
617 (process-status "shell") | |
618 @result{} run | |
619 @end group | |
620 @group | |
621 (process-status (get-buffer "*shell*")) | |
622 @result{} run | |
623 @end group | |
624 @group | |
625 x | |
626 @result{} #<process xx<1>> | |
627 (process-status x) | |
628 @result{} exit | |
629 @end group | |
630 @end smallexample | |
631 | |
632 For a network connection, @code{process-status} returns one of the symbols | |
633 @code{open} or @code{closed}. The latter means that the other side | |
634 closed the connection, or Emacs did @code{delete-process}. | |
635 @end defun | |
636 | |
637 @defun process-exit-status process | |
638 This function returns the exit status of @var{process} or the signal | |
639 number that killed it. (Use the result of @code{process-status} to | |
640 determine which of those it is.) If @var{process} has not yet | |
641 terminated, the value is 0. | |
642 @end defun | |
643 | |
12067 | 644 @defun process-tty-name process |
645 This function returns the terminal name that @var{process} is using for | |
646 its communication with Emacs---or @code{nil} if it is using pipes | |
647 instead of a terminal (see @code{process-connection-type} in | |
648 @ref{Asynchronous Processes}). | |
649 @end defun | |
650 | |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
651 @defun process-coding-system process |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
652 @tindex process-coding-system |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
653 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
|
654 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
|
655 @var{process} (@pxref{Coding Systems}). The value has this form: |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
656 |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
657 @example |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
658 (@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
|
659 @end example |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
660 @end defun |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
661 |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
662 @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
|
663 @tindex set-process-coding-system |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
664 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
|
665 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
|
666 decode subprocess output, and @var{encoding-system} to encode subprocess |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
667 input. |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
668 @end defun |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
669 |
6558 | 670 @node Input to Processes |
671 @section Sending Input to Processes | |
672 @cindex process input | |
673 | |
674 Asynchronous subprocesses receive input when it is sent to them by | |
675 Emacs, which is done with the functions in this section. You must | |
676 specify the process to send input to, and the input data to send. The | |
677 data appears on the ``standard input'' of the subprocess. | |
678 | |
679 Some operating systems have limited space for buffered input in a | |
680 @sc{pty}. On these systems, Emacs sends an @sc{eof} periodically amidst | |
681 the other characters, to force them through. For most programs, | |
682 these @sc{eof}s do no harm. | |
683 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
684 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
|
685 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
|
686 @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
|
687 (@pxref{Process Information}). Otherwise, the coding system comes from |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
688 @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
|
689 the defaulting mechanism (@pxref{Default Coding Systems}). |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
690 |
6558 | 691 @defun process-send-string process-name string |
692 This function sends @var{process-name} the contents of @var{string} as | |
693 standard input. The argument @var{process-name} must be a process or | |
694 the name of a process. If it is @code{nil}, the current buffer's | |
695 process is used. | |
696 | |
697 The function returns @code{nil}. | |
698 | |
699 @smallexample | |
700 @group | |
701 (process-send-string "shell<1>" "ls\n") | |
702 @result{} nil | |
703 @end group | |
704 | |
705 | |
706 @group | |
707 ---------- Buffer: *shell* ---------- | |
708 ... | |
709 introduction.texi syntax-tables.texi~ | |
710 introduction.texi~ text.texi | |
711 introduction.txt text.texi~ | |
712 ... | |
713 ---------- Buffer: *shell* ---------- | |
714 @end group | |
715 @end smallexample | |
716 @end defun | |
717 | |
718 @deffn Command process-send-region process-name start end | |
719 This function sends the text in the region defined by @var{start} and | |
720 @var{end} as standard input to @var{process-name}, which is a process or | |
721 a process name. (If it is @code{nil}, the current buffer's process is | |
722 used.) | |
723 | |
724 An error is signaled unless both @var{start} and @var{end} are | |
725 integers or markers that indicate positions in the current buffer. (It | |
726 is unimportant which number is larger.) | |
727 @end deffn | |
728 | |
729 @defun process-send-eof &optional process-name | |
730 This function makes @var{process-name} see an end-of-file in its | |
731 input. The @sc{eof} comes after any text already sent to it. | |
732 | |
733 If @var{process-name} is not supplied, or if it is @code{nil}, then | |
734 this function sends the @sc{eof} to the current buffer's process. An | |
735 error is signaled if the current buffer has no process. | |
736 | |
737 The function returns @var{process-name}. | |
738 | |
739 @smallexample | |
740 @group | |
741 (process-send-eof "shell") | |
742 @result{} "shell" | |
743 @end group | |
744 @end smallexample | |
745 @end defun | |
746 | |
747 @node Signals to Processes | |
748 @section Sending Signals to Processes | |
749 @cindex process signals | |
750 @cindex sending signals | |
751 @cindex signals | |
752 | |
753 @dfn{Sending a signal} to a subprocess is a way of interrupting its | |
754 activities. There are several different signals, each with its own | |
755 meaning. The set of signals and their names is defined by the operating | |
756 system. For example, the signal @code{SIGINT} means that the user has | |
757 typed @kbd{C-c}, or that some analogous thing has happened. | |
758 | |
759 Each signal has a standard effect on the subprocess. Most signals | |
760 kill the subprocess, but some stop or resume execution instead. Most | |
761 signals can optionally be handled by programs; if the program handles | |
762 the signal, then we can say nothing in general about its effects. | |
763 | |
764 You can send signals explicitly by calling the functions in this | |
765 section. Emacs also sends signals automatically at certain times: | |
766 killing a buffer sends a @code{SIGHUP} signal to all its associated | |
767 processes; killing Emacs sends a @code{SIGHUP} signal to all remaining | |
768 processes. (@code{SIGHUP} is a signal that usually indicates that the | |
769 user hung up the phone.) | |
770 | |
771 Each of the signal-sending functions takes two optional arguments: | |
772 @var{process-name} and @var{current-group}. | |
773 | |
774 The argument @var{process-name} must be either a process, the name of | |
775 one, or @code{nil}. If it is @code{nil}, the process defaults to the | |
776 process associated with the current buffer. An error is signaled if | |
777 @var{process-name} does not identify a process. | |
778 | |
779 The argument @var{current-group} is a flag that makes a difference | |
780 when you are running a job-control shell as an Emacs subprocess. If it | |
781 is non-@code{nil}, then the signal is sent to the current process-group | |
9009 | 782 of the terminal that Emacs uses to communicate with the subprocess. If |
6558 | 783 the process is a job-control shell, this means the shell's current |
784 subjob. If it is @code{nil}, the signal is sent to the process group of | |
785 the immediate subprocess of Emacs. If the subprocess is a job-control | |
786 shell, this is the shell itself. | |
787 | |
788 The flag @var{current-group} has no effect when a pipe is used to | |
789 communicate with the subprocess, because the operating system does not | |
790 support the distinction in the case of pipes. For the same reason, | |
791 job-control shells won't work when a pipe is used. See | |
792 @code{process-connection-type} in @ref{Asynchronous Processes}. | |
793 | |
794 @defun interrupt-process &optional process-name current-group | |
795 This function interrupts the process @var{process-name} by sending the | |
796 signal @code{SIGINT}. Outside of Emacs, typing the ``interrupt | |
797 character'' (normally @kbd{C-c} on some systems, and @code{DEL} on | |
798 others) sends this signal. When the argument @var{current-group} is | |
799 non-@code{nil}, you can think of this function as ``typing @kbd{C-c}'' | |
800 on the terminal by which Emacs talks to the subprocess. | |
801 @end defun | |
802 | |
803 @defun kill-process &optional process-name current-group | |
804 This function kills the process @var{process-name} by sending the | |
805 signal @code{SIGKILL}. This signal kills the subprocess immediately, | |
806 and cannot be handled by the subprocess. | |
807 @end defun | |
808 | |
809 @defun quit-process &optional process-name current-group | |
810 This function sends the signal @code{SIGQUIT} to the process | |
811 @var{process-name}. This signal is the one sent by the ``quit | |
812 character'' (usually @kbd{C-b} or @kbd{C-\}) when you are not inside | |
813 Emacs. | |
814 @end defun | |
815 | |
816 @defun stop-process &optional process-name current-group | |
817 This function stops the process @var{process-name} by sending the | |
818 signal @code{SIGTSTP}. Use @code{continue-process} to resume its | |
819 execution. | |
820 | |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
821 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
|
822 (usually @kbd{C-z}) normally sends this signal. When |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
823 @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
|
824 ``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
|
825 subprocess. |
6558 | 826 @end defun |
827 | |
828 @defun continue-process &optional process-name current-group | |
829 This function resumes execution of the process @var{process} by sending | |
830 it the signal @code{SIGCONT}. This presumes that @var{process-name} was | |
831 stopped previously. | |
832 @end defun | |
833 | |
834 @c Emacs 19 feature | |
835 @defun signal-process pid signal | |
836 This function sends a signal to process @var{pid}, which need not be | |
837 a child of Emacs. The argument @var{signal} specifies which signal | |
838 to send; it should be an integer. | |
839 @end defun | |
840 | |
841 @node Output from Processes | |
842 @section Receiving Output from Processes | |
843 @cindex process output | |
844 @cindex output from processes | |
845 | |
846 There are two ways to receive the output that a subprocess writes to | |
847 its standard output stream. The output can be inserted in a buffer, | |
848 which is called the associated buffer of the process, or a function | |
9009 | 849 called the @dfn{filter function} can be called to act on the output. If |
850 the process has no buffer and no filter function, its output is | |
851 discarded. | |
6558 | 852 |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
853 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
|
854 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
|
855 (@pxref{Waiting}), and in @code{accept-process-output} (@pxref{Accepting |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
856 Output}). This minimizes the problem of timing errors that usually |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
857 plague parallel programming. For example, you can safely create a |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
858 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
|
859 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
|
860 primitive that waits. |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
861 |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
862 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
|
863 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
|
864 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
|
865 system to use (@pxref{Process Information}). Otherwise, the coding |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
866 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
|
867 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
|
868 Coding Systems}). |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
869 |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
870 @strong{Warning:} Coding systems such as @code{undecided} which |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
871 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
|
872 with asynchronous subprocess output. This is because Emacs has to |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
873 process asynchronous subprocess output in batches, as it arrives. Emacs |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
874 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
|
875 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
|
876 coding system which determines both the character code conversion and |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
877 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
|
878 rather than @code{undecided} or @code{latin-1}. |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
879 |
6558 | 880 @menu |
881 * Process Buffers:: If no filter, output is put in a buffer. | |
882 * Filter Functions:: Filter functions accept output from the process. | |
883 * Accepting Output:: Explicitly permitting subprocess output. | |
884 Waiting for subprocess output. | |
885 @end menu | |
886 | |
887 @node Process Buffers | |
888 @subsection Process Buffers | |
889 | |
890 A process can (and usually does) have an @dfn{associated buffer}, | |
891 which is an ordinary Emacs buffer that is used for two purposes: storing | |
892 the output from the process, and deciding when to kill the process. You | |
893 can also use the buffer to identify a process to operate on, since in | |
894 normal practice only one process is associated with any given buffer. | |
895 Many applications of processes also use the buffer for editing input to | |
896 be sent to the process, but this is not built into Emacs Lisp. | |
897 | |
898 Unless the process has a filter function (@pxref{Filter Functions}), | |
899 its output is inserted in the associated buffer. The position to insert | |
9009 | 900 the output is determined by the @code{process-mark}, which is then |
901 updated to point to the end of the text just inserted. Usually, but not | |
902 always, the @code{process-mark} is at the end of the buffer. | |
6558 | 903 |
904 @defun process-buffer process | |
905 This function returns the associated buffer of the process | |
906 @var{process}. | |
907 | |
908 @smallexample | |
909 @group | |
910 (process-buffer (get-process "shell")) | |
911 @result{} #<buffer *shell*> | |
912 @end group | |
913 @end smallexample | |
914 @end defun | |
915 | |
916 @defun process-mark process | |
917 This function returns the process marker for @var{process}, which is the | |
918 marker that says where to insert output from the process. | |
919 | |
920 If @var{process} does not have a buffer, @code{process-mark} returns a | |
921 marker that points nowhere. | |
922 | |
923 Insertion of process output in a buffer uses this marker to decide where | |
924 to insert, and updates it to point after the inserted text. That is why | |
925 successive batches of output are inserted consecutively. | |
926 | |
927 Filter functions normally should use this marker in the same fashion | |
928 as is done by direct insertion of output in the buffer. A good | |
929 example of a filter function that uses @code{process-mark} is found at | |
930 the end of the following section. | |
931 | |
932 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
|
933 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
|
934 from previous output. |
6558 | 935 @end defun |
936 | |
937 @defun set-process-buffer process buffer | |
938 This function sets the buffer associated with @var{process} to | |
939 @var{buffer}. If @var{buffer} is @code{nil}, the process becomes | |
940 associated with no buffer. | |
941 @end defun | |
942 | |
943 @defun get-buffer-process buffer-or-name | |
944 This function returns the process associated with @var{buffer-or-name}. | |
945 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
|
946 (Currently, the one chosen is the one most recently created.) It is |
6558 | 947 usually a bad idea to have more than one process associated with the |
948 same buffer. | |
949 | |
950 @smallexample | |
951 @group | |
952 (get-buffer-process "*shell*") | |
953 @result{} #<process shell> | |
954 @end group | |
955 @end smallexample | |
956 | |
957 Killing the process's buffer deletes the process, which kills the | |
958 subprocess with a @code{SIGHUP} signal (@pxref{Signals to Processes}). | |
959 @end defun | |
960 | |
961 @node Filter Functions | |
962 @subsection Process Filter Functions | |
963 @cindex filter function | |
964 @cindex process filter | |
965 | |
966 A process @dfn{filter function} is a function that receives the | |
967 standard output from the associated process. If a process has a filter, | |
9009 | 968 then @emph{all} output from that process is passed to the filter. The |
969 process buffer is used directly for output from the process only when | |
970 there is no filter. | |
6558 | 971 |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
972 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
|
973 something, because process output arrives only at such times. Emacs |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
974 waits when reading terminal input, in @code{sit-for} and |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
975 @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
|
976 (@pxref{Accepting Output}). |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
977 |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
978 A filter function must accept two arguments: the associated process |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
979 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
|
980 then free to do whatever it chooses with the output. |
6558 | 981 |
982 Quitting is normally inhibited within a filter function---otherwise, | |
983 the effect of typing @kbd{C-g} at command level or to quit a user | |
984 command would be unpredictable. If you want to permit quitting inside a | |
985 filter function, bind @code{inhibit-quit} to @code{nil}. | |
986 @xref{Quitting}. | |
987 | |
12067 | 988 If an error happens during execution of a filter function, it is |
989 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
|
990 program was running when the filter function was started. However, if |
12067 | 991 @code{debug-on-error} is non-@code{nil}, the error-catching is turned |
992 off. This makes it possible to use the Lisp debugger to debug the | |
993 filter function. @xref{Debugger}. | |
994 | |
6558 | 995 Many filter functions sometimes or always insert the text in the |
996 process's buffer, mimicking the actions of Emacs when there is no | |
997 filter. Such filter functions need to use @code{set-buffer} in order to | |
998 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
|
999 semipermanently, these filter functions must save and restore the |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1000 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
|
1001 cases update the value of point. Here is how to do these things: |
6558 | 1002 |
1003 @smallexample | |
1004 @group | |
1005 (defun ordinary-insertion-filter (proc string) | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1006 (with-current-buffer (process-buffer proc) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1007 (let ((moving (= (point) (process-mark proc)))) |
6558 | 1008 @end group |
1009 @group | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1010 (save-excursion |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1011 ;; @r{Insert the text, advancing the process marker.} |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1012 (goto-char (process-mark proc)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1013 (insert string) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1014 (set-marker (process-mark proc) (point))) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1015 (if moving (goto-char (process-mark proc)))))) |
6558 | 1016 @end group |
1017 @end smallexample | |
1018 | |
1019 @noindent | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1020 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
|
1021 @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
|
1022 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
|
1023 @code{goto-char}. |
6558 | 1024 |
1025 To make the filter force the process buffer to be visible whenever new | |
1026 text arrives, insert the following line just before the | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1027 @code{with-current-buffer} construct: |
6558 | 1028 |
1029 @smallexample | |
1030 (display-buffer (process-buffer proc)) | |
1031 @end smallexample | |
1032 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1033 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
|
1034 previously, eliminate the variable @code{moving} and call |
6558 | 1035 @code{goto-char} unconditionally. |
1036 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1037 In earlier Emacs versions, every filter function that did regular |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1038 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
|
1039 match data. Now Emacs does this automatically for filter functions; |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1040 they never need to do it explicitly. @xref{Match Data}. |
6558 | 1041 |
1042 A filter function that writes the output into the buffer of the | |
12098 | 1043 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
|
1044 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
|
1045 @code{(buffer-name (process-buffer @var{process}))} returns @code{nil} |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1046 if the buffer is dead. |
6558 | 1047 |
1048 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
|
1049 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
|
1050 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
|
1051 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
|
1052 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
|
1053 or more batches of output. |
6558 | 1054 |
1055 @defun set-process-filter process filter | |
1056 This function gives @var{process} the filter function @var{filter}. If | |
1057 @var{filter} is @code{nil}, it gives the process no filter. | |
1058 @end defun | |
1059 | |
1060 @defun process-filter process | |
1061 This function returns the filter function of @var{process}, or @code{nil} | |
1062 if it has none. | |
1063 @end defun | |
1064 | |
1065 Here is an example of use of a filter function: | |
1066 | |
1067 @smallexample | |
1068 @group | |
1069 (defun keep-output (process output) | |
1070 (setq kept (cons output kept))) | |
1071 @result{} keep-output | |
1072 @end group | |
1073 @group | |
1074 (setq kept nil) | |
1075 @result{} nil | |
1076 @end group | |
1077 @group | |
1078 (set-process-filter (get-process "shell") 'keep-output) | |
1079 @result{} keep-output | |
1080 @end group | |
1081 @group | |
1082 (process-send-string "shell" "ls ~/other\n") | |
1083 @result{} nil | |
1084 kept | |
1085 @result{} ("lewis@@slug[8] % " | |
1086 @end group | |
1087 @group | |
1088 "FINAL-W87-SHORT.MSS backup.otl kolstad.mss~ | |
1089 address.txt backup.psf kolstad.psf | |
1090 backup.bib~ david.mss resume-Dec-86.mss~ | |
1091 backup.err david.psf resume-Dec.psf | |
1092 backup.mss dland syllabus.mss | |
1093 " | |
1094 "#backups.mss# backup.mss~ kolstad.mss | |
1095 ") | |
1096 @end group | |
1097 @end smallexample | |
1098 | |
1099 @ignore @c The code in this example doesn't show the right way to do things. | |
1100 Here is another, more realistic example, which demonstrates how to use | |
1101 the process mark to do insertion in the same fashion as is done when | |
1102 there is no filter function: | |
1103 | |
1104 @smallexample | |
1105 @group | |
1106 ;; @r{Insert input in the buffer specified by @code{my-shell-buffer}} | |
1107 ;; @r{and make sure that buffer is shown in some window.} | |
1108 (defun my-process-filter (proc str) | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1109 (let ((cur (selected-window)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1110 (pop-up-windows t)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1111 (pop-to-buffer my-shell-buffer) |
6558 | 1112 @end group |
1113 @group | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1114 (goto-char (point-max)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1115 (insert str) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1116 (set-marker (process-mark proc) (point-max)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1117 (select-window cur))) |
6558 | 1118 @end group |
1119 @end smallexample | |
1120 @end ignore | |
1121 | |
1122 @node Accepting Output | |
1123 @subsection Accepting Output from Processes | |
1124 | |
1125 Output from asynchronous subprocesses normally arrives only while | |
1126 Emacs is waiting for some sort of external event, such as elapsed time | |
1127 or terminal input. Occasionally it is useful in a Lisp program to | |
1128 explicitly permit output to arrive at a specific point, or even to wait | |
1129 until output arrives from a process. | |
1130 | |
1131 @defun accept-process-output &optional process seconds millisec | |
1132 This function allows Emacs to read pending output from processes. The | |
1133 output is inserted in the associated buffers or given to their filter | |
1134 functions. If @var{process} is non-@code{nil} then this function does | |
1135 not return until some output has been received from @var{process}. | |
1136 | |
1137 @c Emacs 19 feature | |
1138 The arguments @var{seconds} and @var{millisec} let you specify timeout | |
1139 periods. The former specifies a period measured in seconds and the | |
1140 latter specifies one measured in milliseconds. The two time periods | |
1141 thus specified are added together, and @code{accept-process-output} | |
1142 returns after that much time whether or not there has been any | |
1143 subprocess output. | |
1144 | |
12098 | 1145 The argument @var{seconds} need not be an integer. If it is a floating |
1146 point number, this function waits for a fractional number of seconds. | |
1147 Some systems support only a whole number of seconds; on these systems, | |
1148 @var{seconds} is rounded down. If the system doesn't support waiting | |
1149 fractions of a second, you get an error if you specify nonzero | |
1150 @var{millisec}. | |
1151 | |
6558 | 1152 Not all operating systems support waiting periods other than multiples |
1153 of a second; on those that do not, you get an error if you specify | |
1154 nonzero @var{millisec}. | |
1155 | |
1156 The function @code{accept-process-output} returns non-@code{nil} if it | |
1157 did get some output, or @code{nil} if the timeout expired before output | |
1158 arrived. | |
1159 @end defun | |
1160 | |
1161 @node Sentinels | |
1162 @section Sentinels: Detecting Process Status Changes | |
1163 @cindex process sentinel | |
1164 @cindex sentinel | |
1165 | |
1166 A @dfn{process sentinel} is a function that is called whenever the | |
1167 associated process changes status for any reason, including signals | |
1168 (whether sent by Emacs or caused by the process's own actions) that | |
1169 terminate, stop, or continue the process. The process sentinel is also | |
1170 called if the process exits. The sentinel receives two arguments: the | |
1171 process for which the event occurred, and a string describing the type | |
1172 of event. | |
1173 | |
1174 The string describing the event looks like one of the following: | |
1175 | |
1176 @itemize @bullet | |
1177 @item | |
1178 @code{"finished\n"}. | |
1179 | |
1180 @item | |
1181 @code{"exited abnormally with code @var{exitcode}\n"}. | |
1182 | |
1183 @item | |
1184 @code{"@var{name-of-signal}\n"}. | |
1185 | |
1186 @item | |
1187 @code{"@var{name-of-signal} (core dumped)\n"}. | |
1188 @end itemize | |
1189 | |
1190 A sentinel runs only while Emacs is waiting (e.g., for terminal input, | |
1191 or for time to elapse, or for process output). This avoids the timing | |
1192 errors that could result from running them at random places in the | |
1193 middle of other Lisp programs. A program can wait, so that sentinels | |
9009 | 1194 will run, by calling @code{sit-for} or @code{sleep-for} |
1195 (@pxref{Waiting}), or @code{accept-process-output} (@pxref{Accepting | |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
1196 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
|
1197 reading input. |
6558 | 1198 |
1199 Quitting is normally inhibited within a sentinel---otherwise, the | |
1200 effect of typing @kbd{C-g} at command level or to quit a user command | |
1201 would be unpredictable. If you want to permit quitting inside a | |
1202 sentinel, bind @code{inhibit-quit} to @code{nil}. @xref{Quitting}. | |
1203 | |
1204 A sentinel that writes the output into the buffer of the process | |
12098 | 1205 should check whether the buffer is still alive. If it tries to insert |
6558 | 1206 into a dead buffer, it will get an error. If the buffer is dead, |
1207 @code{(buffer-name (process-buffer @var{process}))} returns @code{nil}. | |
1208 | |
12067 | 1209 If an error happens during execution of a sentinel, it is caught |
1210 automatically, so that it doesn't stop the execution of whatever | |
1211 programs was running when the sentinel was started. However, if | |
1212 @code{debug-on-error} is non-@code{nil}, the error-catching is turned | |
1213 off. This makes it possible to use the Lisp debugger to debug the | |
1214 sentinel. @xref{Debugger}. | |
1215 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1216 In earlier Emacs versions, every sentinel that did regular expression |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1217 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
|
1218 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
|
1219 it explicitly. @xref{Match Data}. |
12098 | 1220 |
6558 | 1221 @defun set-process-sentinel process sentinel |
1222 This function associates @var{sentinel} with @var{process}. If | |
1223 @var{sentinel} is @code{nil}, then the process will have no sentinel. | |
1224 The default behavior when there is no sentinel is to insert a message in | |
1225 the process's buffer when the process status changes. | |
1226 | |
1227 @smallexample | |
1228 @group | |
1229 (defun msg-me (process event) | |
1230 (princ | |
1231 (format "Process: %s had the event `%s'" process event))) | |
1232 (set-process-sentinel (get-process "shell") 'msg-me) | |
1233 @result{} msg-me | |
1234 @end group | |
1235 @group | |
1236 (kill-process (get-process "shell")) | |
1237 @print{} Process: #<process shell> had the event `killed' | |
1238 @result{} #<process shell> | |
1239 @end group | |
1240 @end smallexample | |
1241 @end defun | |
1242 | |
1243 @defun process-sentinel process | |
1244 This function returns the sentinel of @var{process}, or @code{nil} if it | |
1245 has none. | |
1246 @end defun | |
1247 | |
1248 @defun waiting-for-user-input-p | |
1249 While a sentinel or filter function is running, this function returns | |
1250 non-@code{nil} if Emacs was waiting for keyboard input from the user at | |
1251 the time the sentinel or filter function was called, @code{nil} if it | |
1252 was not. | |
1253 @end defun | |
1254 | |
1255 @node Transaction Queues | |
1256 @section Transaction Queues | |
1257 @cindex transaction queue | |
1258 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1259 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
|
1260 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
|
1261 queue communicating with a specified process. Then you can call |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1262 @code{tq-enqueue} to send a transaction. |
6558 | 1263 |
1264 @defun tq-create process | |
1265 This function creates and returns a transaction queue communicating with | |
1266 @var{process}. The argument @var{process} should be a subprocess | |
1267 capable of sending and receiving streams of bytes. It may be a child | |
9009 | 1268 process, or it may be a TCP connection to a server, possibly on another |
6558 | 1269 machine. |
1270 @end defun | |
1271 | |
1272 @defun tq-enqueue queue question regexp closure fn | |
1273 This function sends a transaction to queue @var{queue}. Specifying the | |
1274 queue has the effect of specifying the subprocess to talk to. | |
1275 | |
9009 | 1276 The argument @var{question} is the outgoing message that starts the |
6558 | 1277 transaction. The argument @var{fn} is the function to call when the |
1278 corresponding answer comes back; it is called with two arguments: | |
1279 @var{closure}, and the answer received. | |
1280 | |
1281 The argument @var{regexp} is a regular expression that should match the | |
1282 entire answer, but nothing less; that's how @code{tq-enqueue} determines | |
1283 where the answer ends. | |
1284 | |
1285 The return value of @code{tq-enqueue} itself is not meaningful. | |
1286 @end defun | |
1287 | |
1288 @defun tq-close queue | |
1289 Shut down transaction queue @var{queue}, waiting for all pending transactions | |
1290 to complete, and then terminate the connection or child process. | |
1291 @end defun | |
1292 | |
1293 Transaction queues are implemented by means of a filter function. | |
1294 @xref{Filter Functions}. | |
1295 | |
12098 | 1296 @node Network |
1297 @section Network Connections | |
1298 @cindex network connection | |
6558 | 1299 @cindex TCP |
1300 | |
12098 | 1301 Emacs Lisp programs can open TCP network connections to other processes on |
1302 the same machine or other machines. A network connection is handled by Lisp | |
6558 | 1303 much like a subprocess, and is represented by a process object. |
1304 However, the process you are communicating with is not a child of the | |
1305 Emacs process, so you can't kill it or send it signals. All you can do | |
1306 is send and receive data. @code{delete-process} closes the connection, | |
1307 but does not kill the process at the other end; that process must decide | |
1308 what to do about closure of the connection. | |
1309 | |
1310 You can distinguish process objects representing network connections | |
1311 from those representing subprocesses with the @code{process-status} | |
12098 | 1312 function. It always returns either @code{open} or @code{closed} for a |
1313 network connection, and it never returns either of those values for a | |
1314 real subprocess. @xref{Process Information}. | |
6558 | 1315 |
1316 @defun open-network-stream name buffer-or-name host service | |
1317 This function opens a TCP connection for a service to a host. It | |
1318 returns a process object to represent the connection. | |
1319 | |
1320 The @var{name} argument specifies the name for the process object. It | |
1321 is modified as necessary to make it unique. | |
1322 | |
1323 The @var{buffer-or-name} argument is the buffer to associate with the | |
1324 connection. Output from the connection is inserted in the buffer, | |
1325 unless you specify a filter function to handle the output. If | |
1326 @var{buffer-or-name} is @code{nil}, it means that the connection is not | |
1327 associated with any buffer. | |
1328 | |
1329 The arguments @var{host} and @var{service} specify where to connect to; | |
1330 @var{host} is the host name (a string), and @var{service} is the name of | |
1331 a defined network service (a string) or a port number (an integer). | |
1332 @end defun |