Mercurial > emacs
annotate lispref/processes.texi @ 23031:5e2bc240edfc
(language-info-alist): Doc-string
modified.
(set-language-info-alist): Fix typo in doc-string. Update
setup-language-environment-map unconditionally.
(mule-keymap): Key bindings for set-selection-coding-system and
set-next-selection-coding-system.
(set-coding-system-map): Add items of set-selection-coding-system
and set-next-selection-coding-system.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Sat, 15 Aug 1998 01:28:14 +0000 |
parents | dfac7398266b |
children | 7451b1458af1 |
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 |
6558 | 696 @defun process-send-string process-name string |
697 This function sends @var{process-name} the contents of @var{string} as | |
698 standard input. The argument @var{process-name} must be a process or | |
699 the name of a process. If it is @code{nil}, the current buffer's | |
700 process is used. | |
701 | |
702 The function returns @code{nil}. | |
703 | |
704 @smallexample | |
705 @group | |
706 (process-send-string "shell<1>" "ls\n") | |
707 @result{} nil | |
708 @end group | |
709 | |
710 | |
711 @group | |
712 ---------- Buffer: *shell* ---------- | |
713 ... | |
714 introduction.texi syntax-tables.texi~ | |
715 introduction.texi~ text.texi | |
716 introduction.txt text.texi~ | |
717 ... | |
718 ---------- Buffer: *shell* ---------- | |
719 @end group | |
720 @end smallexample | |
721 @end defun | |
722 | |
723 @deffn Command process-send-region process-name start end | |
724 This function sends the text in the region defined by @var{start} and | |
725 @var{end} as standard input to @var{process-name}, which is a process or | |
726 a process name. (If it is @code{nil}, the current buffer's process is | |
727 used.) | |
728 | |
729 An error is signaled unless both @var{start} and @var{end} are | |
730 integers or markers that indicate positions in the current buffer. (It | |
731 is unimportant which number is larger.) | |
732 @end deffn | |
733 | |
734 @defun process-send-eof &optional process-name | |
735 This function makes @var{process-name} see an end-of-file in its | |
736 input. The @sc{eof} comes after any text already sent to it. | |
737 | |
738 If @var{process-name} is not supplied, or if it is @code{nil}, then | |
739 this function sends the @sc{eof} to the current buffer's process. An | |
740 error is signaled if the current buffer has no process. | |
741 | |
742 The function returns @var{process-name}. | |
743 | |
744 @smallexample | |
745 @group | |
746 (process-send-eof "shell") | |
747 @result{} "shell" | |
748 @end group | |
749 @end smallexample | |
750 @end defun | |
751 | |
752 @node Signals to Processes | |
753 @section Sending Signals to Processes | |
754 @cindex process signals | |
755 @cindex sending signals | |
756 @cindex signals | |
757 | |
758 @dfn{Sending a signal} to a subprocess is a way of interrupting its | |
759 activities. There are several different signals, each with its own | |
760 meaning. The set of signals and their names is defined by the operating | |
761 system. For example, the signal @code{SIGINT} means that the user has | |
762 typed @kbd{C-c}, or that some analogous thing has happened. | |
763 | |
764 Each signal has a standard effect on the subprocess. Most signals | |
765 kill the subprocess, but some stop or resume execution instead. Most | |
766 signals can optionally be handled by programs; if the program handles | |
767 the signal, then we can say nothing in general about its effects. | |
768 | |
769 You can send signals explicitly by calling the functions in this | |
770 section. Emacs also sends signals automatically at certain times: | |
771 killing a buffer sends a @code{SIGHUP} signal to all its associated | |
772 processes; killing Emacs sends a @code{SIGHUP} signal to all remaining | |
773 processes. (@code{SIGHUP} is a signal that usually indicates that the | |
774 user hung up the phone.) | |
775 | |
776 Each of the signal-sending functions takes two optional arguments: | |
777 @var{process-name} and @var{current-group}. | |
778 | |
779 The argument @var{process-name} must be either a process, the name of | |
780 one, or @code{nil}. If it is @code{nil}, the process defaults to the | |
781 process associated with the current buffer. An error is signaled if | |
782 @var{process-name} does not identify a process. | |
783 | |
784 The argument @var{current-group} is a flag that makes a difference | |
785 when you are running a job-control shell as an Emacs subprocess. If it | |
786 is non-@code{nil}, then the signal is sent to the current process-group | |
9009 | 787 of the terminal that Emacs uses to communicate with the subprocess. If |
6558 | 788 the process is a job-control shell, this means the shell's current |
789 subjob. If it is @code{nil}, the signal is sent to the process group of | |
790 the immediate subprocess of Emacs. If the subprocess is a job-control | |
791 shell, this is the shell itself. | |
792 | |
793 The flag @var{current-group} has no effect when a pipe is used to | |
794 communicate with the subprocess, because the operating system does not | |
795 support the distinction in the case of pipes. For the same reason, | |
796 job-control shells won't work when a pipe is used. See | |
797 @code{process-connection-type} in @ref{Asynchronous Processes}. | |
798 | |
799 @defun interrupt-process &optional process-name current-group | |
800 This function interrupts the process @var{process-name} by sending the | |
801 signal @code{SIGINT}. Outside of Emacs, typing the ``interrupt | |
802 character'' (normally @kbd{C-c} on some systems, and @code{DEL} on | |
803 others) sends this signal. When the argument @var{current-group} is | |
804 non-@code{nil}, you can think of this function as ``typing @kbd{C-c}'' | |
805 on the terminal by which Emacs talks to the subprocess. | |
806 @end defun | |
807 | |
808 @defun kill-process &optional process-name current-group | |
809 This function kills the process @var{process-name} by sending the | |
810 signal @code{SIGKILL}. This signal kills the subprocess immediately, | |
811 and cannot be handled by the subprocess. | |
812 @end defun | |
813 | |
814 @defun quit-process &optional process-name current-group | |
815 This function sends the signal @code{SIGQUIT} to the process | |
816 @var{process-name}. This signal is the one sent by the ``quit | |
817 character'' (usually @kbd{C-b} or @kbd{C-\}) when you are not inside | |
818 Emacs. | |
819 @end defun | |
820 | |
821 @defun stop-process &optional process-name current-group | |
822 This function stops the process @var{process-name} by sending the | |
823 signal @code{SIGTSTP}. Use @code{continue-process} to resume its | |
824 execution. | |
825 | |
21682
90da2489c498
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21007
diff
changeset
|
826 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
|
827 (usually @kbd{C-z}) normally sends this signal. When |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
828 @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
|
829 ``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
|
830 subprocess. |
6558 | 831 @end defun |
832 | |
833 @defun continue-process &optional process-name current-group | |
834 This function resumes execution of the process @var{process} by sending | |
835 it the signal @code{SIGCONT}. This presumes that @var{process-name} was | |
836 stopped previously. | |
837 @end defun | |
838 | |
839 @c Emacs 19 feature | |
840 @defun signal-process pid signal | |
841 This function sends a signal to process @var{pid}, which need not be | |
842 a child of Emacs. The argument @var{signal} specifies which signal | |
843 to send; it should be an integer. | |
844 @end defun | |
845 | |
846 @node Output from Processes | |
847 @section Receiving Output from Processes | |
848 @cindex process output | |
849 @cindex output from processes | |
850 | |
851 There are two ways to receive the output that a subprocess writes to | |
852 its standard output stream. The output can be inserted in a buffer, | |
853 which is called the associated buffer of the process, or a function | |
9009 | 854 called the @dfn{filter function} can be called to act on the output. If |
855 the process has no buffer and no filter function, its output is | |
856 discarded. | |
6558 | 857 |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
858 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
|
859 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
|
860 (@pxref{Waiting}), and in @code{accept-process-output} (@pxref{Accepting |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
861 Output}). This minimizes the problem of timing errors that usually |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
862 plague parallel programming. For example, you can safely create a |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
863 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
|
864 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
|
865 primitive that waits. |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
866 |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
867 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
|
868 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
|
869 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
|
870 system to use (@pxref{Process Information}). Otherwise, the coding |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
871 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
|
872 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
|
873 Coding Systems}). |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
874 |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
875 @strong{Warning:} Coding systems such as @code{undecided} which |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
876 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
|
877 with asynchronous subprocess output. This is because Emacs has to |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
878 process asynchronous subprocess output in batches, as it arrives. Emacs |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
879 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
|
880 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
|
881 coding system which determines both the character code conversion and |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
882 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
|
883 rather than @code{undecided} or @code{latin-1}. |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
884 |
6558 | 885 @menu |
886 * Process Buffers:: If no filter, output is put in a buffer. | |
887 * Filter Functions:: Filter functions accept output from the process. | |
888 * Accepting Output:: Explicitly permitting subprocess output. | |
889 Waiting for subprocess output. | |
890 @end menu | |
891 | |
892 @node Process Buffers | |
893 @subsection Process Buffers | |
894 | |
895 A process can (and usually does) have an @dfn{associated buffer}, | |
896 which is an ordinary Emacs buffer that is used for two purposes: storing | |
897 the output from the process, and deciding when to kill the process. You | |
898 can also use the buffer to identify a process to operate on, since in | |
899 normal practice only one process is associated with any given buffer. | |
900 Many applications of processes also use the buffer for editing input to | |
901 be sent to the process, but this is not built into Emacs Lisp. | |
902 | |
903 Unless the process has a filter function (@pxref{Filter Functions}), | |
904 its output is inserted in the associated buffer. The position to insert | |
9009 | 905 the output is determined by the @code{process-mark}, which is then |
906 updated to point to the end of the text just inserted. Usually, but not | |
907 always, the @code{process-mark} is at the end of the buffer. | |
6558 | 908 |
909 @defun process-buffer process | |
910 This function returns the associated buffer of the process | |
911 @var{process}. | |
912 | |
913 @smallexample | |
914 @group | |
915 (process-buffer (get-process "shell")) | |
916 @result{} #<buffer *shell*> | |
917 @end group | |
918 @end smallexample | |
919 @end defun | |
920 | |
921 @defun process-mark process | |
922 This function returns the process marker for @var{process}, which is the | |
923 marker that says where to insert output from the process. | |
924 | |
925 If @var{process} does not have a buffer, @code{process-mark} returns a | |
926 marker that points nowhere. | |
927 | |
928 Insertion of process output in a buffer uses this marker to decide where | |
929 to insert, and updates it to point after the inserted text. That is why | |
930 successive batches of output are inserted consecutively. | |
931 | |
932 Filter functions normally should use this marker in the same fashion | |
933 as is done by direct insertion of output in the buffer. A good | |
934 example of a filter function that uses @code{process-mark} is found at | |
935 the end of the following section. | |
936 | |
937 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
|
938 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
|
939 from previous output. |
6558 | 940 @end defun |
941 | |
942 @defun set-process-buffer process buffer | |
943 This function sets the buffer associated with @var{process} to | |
944 @var{buffer}. If @var{buffer} is @code{nil}, the process becomes | |
945 associated with no buffer. | |
946 @end defun | |
947 | |
948 @defun get-buffer-process buffer-or-name | |
949 This function returns the process associated with @var{buffer-or-name}. | |
950 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
|
951 (Currently, the one chosen is the one most recently created.) It is |
6558 | 952 usually a bad idea to have more than one process associated with the |
953 same buffer. | |
954 | |
955 @smallexample | |
956 @group | |
957 (get-buffer-process "*shell*") | |
958 @result{} #<process shell> | |
959 @end group | |
960 @end smallexample | |
961 | |
962 Killing the process's buffer deletes the process, which kills the | |
963 subprocess with a @code{SIGHUP} signal (@pxref{Signals to Processes}). | |
964 @end defun | |
965 | |
966 @node Filter Functions | |
967 @subsection Process Filter Functions | |
968 @cindex filter function | |
969 @cindex process filter | |
970 | |
971 A process @dfn{filter function} is a function that receives the | |
972 standard output from the associated process. If a process has a filter, | |
9009 | 973 then @emph{all} output from that process is passed to the filter. The |
974 process buffer is used directly for output from the process only when | |
975 there is no filter. | |
6558 | 976 |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
977 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
|
978 something, because process output arrives only at such times. Emacs |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
979 waits when reading terminal input, in @code{sit-for} and |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
980 @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
|
981 (@pxref{Accepting Output}). |
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
982 |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
983 A filter function must accept two arguments: the associated process |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
984 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
|
985 then free to do whatever it chooses with the output. |
6558 | 986 |
987 Quitting is normally inhibited within a filter function---otherwise, | |
988 the effect of typing @kbd{C-g} at command level or to quit a user | |
989 command would be unpredictable. If you want to permit quitting inside a | |
990 filter function, bind @code{inhibit-quit} to @code{nil}. | |
991 @xref{Quitting}. | |
992 | |
12067 | 993 If an error happens during execution of a filter function, it is |
994 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
|
995 program was running when the filter function was started. However, if |
12067 | 996 @code{debug-on-error} is non-@code{nil}, the error-catching is turned |
997 off. This makes it possible to use the Lisp debugger to debug the | |
998 filter function. @xref{Debugger}. | |
999 | |
6558 | 1000 Many filter functions sometimes or always insert the text in the |
1001 process's buffer, mimicking the actions of Emacs when there is no | |
1002 filter. Such filter functions need to use @code{set-buffer} in order to | |
1003 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
|
1004 semipermanently, these filter functions must save and restore the |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1005 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
|
1006 cases update the value of point. Here is how to do these things: |
6558 | 1007 |
1008 @smallexample | |
1009 @group | |
1010 (defun ordinary-insertion-filter (proc string) | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1011 (with-current-buffer (process-buffer proc) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1012 (let ((moving (= (point) (process-mark proc)))) |
6558 | 1013 @end group |
1014 @group | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1015 (save-excursion |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1016 ;; @r{Insert the text, advancing the process marker.} |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1017 (goto-char (process-mark proc)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1018 (insert string) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1019 (set-marker (process-mark proc) (point))) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1020 (if moving (goto-char (process-mark proc)))))) |
6558 | 1021 @end group |
1022 @end smallexample | |
1023 | |
1024 @noindent | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1025 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
|
1026 @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
|
1027 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
|
1028 @code{goto-char}. |
6558 | 1029 |
1030 To make the filter force the process buffer to be visible whenever new | |
1031 text arrives, insert the following line just before the | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1032 @code{with-current-buffer} construct: |
6558 | 1033 |
1034 @smallexample | |
1035 (display-buffer (process-buffer proc)) | |
1036 @end smallexample | |
1037 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1038 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
|
1039 previously, eliminate the variable @code{moving} and call |
6558 | 1040 @code{goto-char} unconditionally. |
1041 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1042 In earlier Emacs versions, every filter function that did regular |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1043 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
|
1044 match data. Now Emacs does this automatically for filter functions; |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1045 they never need to do it explicitly. @xref{Match Data}. |
6558 | 1046 |
1047 A filter function that writes the output into the buffer of the | |
12098 | 1048 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
|
1049 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
|
1050 @code{(buffer-name (process-buffer @var{process}))} returns @code{nil} |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1051 if the buffer is dead. |
6558 | 1052 |
1053 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
|
1054 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
|
1055 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
|
1056 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
|
1057 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
|
1058 or more batches of output. |
6558 | 1059 |
1060 @defun set-process-filter process filter | |
1061 This function gives @var{process} the filter function @var{filter}. If | |
1062 @var{filter} is @code{nil}, it gives the process no filter. | |
1063 @end defun | |
1064 | |
1065 @defun process-filter process | |
1066 This function returns the filter function of @var{process}, or @code{nil} | |
1067 if it has none. | |
1068 @end defun | |
1069 | |
1070 Here is an example of use of a filter function: | |
1071 | |
1072 @smallexample | |
1073 @group | |
1074 (defun keep-output (process output) | |
1075 (setq kept (cons output kept))) | |
1076 @result{} keep-output | |
1077 @end group | |
1078 @group | |
1079 (setq kept nil) | |
1080 @result{} nil | |
1081 @end group | |
1082 @group | |
1083 (set-process-filter (get-process "shell") 'keep-output) | |
1084 @result{} keep-output | |
1085 @end group | |
1086 @group | |
1087 (process-send-string "shell" "ls ~/other\n") | |
1088 @result{} nil | |
1089 kept | |
1090 @result{} ("lewis@@slug[8] % " | |
1091 @end group | |
1092 @group | |
1093 "FINAL-W87-SHORT.MSS backup.otl kolstad.mss~ | |
1094 address.txt backup.psf kolstad.psf | |
1095 backup.bib~ david.mss resume-Dec-86.mss~ | |
1096 backup.err david.psf resume-Dec.psf | |
1097 backup.mss dland syllabus.mss | |
1098 " | |
1099 "#backups.mss# backup.mss~ kolstad.mss | |
1100 ") | |
1101 @end group | |
1102 @end smallexample | |
1103 | |
1104 @ignore @c The code in this example doesn't show the right way to do things. | |
1105 Here is another, more realistic example, which demonstrates how to use | |
1106 the process mark to do insertion in the same fashion as is done when | |
1107 there is no filter function: | |
1108 | |
1109 @smallexample | |
1110 @group | |
1111 ;; @r{Insert input in the buffer specified by @code{my-shell-buffer}} | |
1112 ;; @r{and make sure that buffer is shown in some window.} | |
1113 (defun my-process-filter (proc str) | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1114 (let ((cur (selected-window)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1115 (pop-up-windows t)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1116 (pop-to-buffer my-shell-buffer) |
6558 | 1117 @end group |
1118 @group | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1119 (goto-char (point-max)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1120 (insert str) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1121 (set-marker (process-mark proc) (point-max)) |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1122 (select-window cur))) |
6558 | 1123 @end group |
1124 @end smallexample | |
1125 @end ignore | |
1126 | |
1127 @node Accepting Output | |
1128 @subsection Accepting Output from Processes | |
1129 | |
1130 Output from asynchronous subprocesses normally arrives only while | |
1131 Emacs is waiting for some sort of external event, such as elapsed time | |
1132 or terminal input. Occasionally it is useful in a Lisp program to | |
1133 explicitly permit output to arrive at a specific point, or even to wait | |
1134 until output arrives from a process. | |
1135 | |
1136 @defun accept-process-output &optional process seconds millisec | |
1137 This function allows Emacs to read pending output from processes. The | |
1138 output is inserted in the associated buffers or given to their filter | |
1139 functions. If @var{process} is non-@code{nil} then this function does | |
1140 not return until some output has been received from @var{process}. | |
1141 | |
1142 @c Emacs 19 feature | |
1143 The arguments @var{seconds} and @var{millisec} let you specify timeout | |
1144 periods. The former specifies a period measured in seconds and the | |
1145 latter specifies one measured in milliseconds. The two time periods | |
1146 thus specified are added together, and @code{accept-process-output} | |
1147 returns after that much time whether or not there has been any | |
1148 subprocess output. | |
1149 | |
12098 | 1150 The argument @var{seconds} need not be an integer. If it is a floating |
1151 point number, this function waits for a fractional number of seconds. | |
1152 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
|
1153 @var{seconds} is rounded down. |
12098 | 1154 |
6558 | 1155 Not all operating systems support waiting periods other than multiples |
1156 of a second; on those that do not, you get an error if you specify | |
1157 nonzero @var{millisec}. | |
1158 | |
1159 The function @code{accept-process-output} returns non-@code{nil} if it | |
1160 did get some output, or @code{nil} if the timeout expired before output | |
1161 arrived. | |
1162 @end defun | |
1163 | |
1164 @node Sentinels | |
1165 @section Sentinels: Detecting Process Status Changes | |
1166 @cindex process sentinel | |
1167 @cindex sentinel | |
1168 | |
1169 A @dfn{process sentinel} is a function that is called whenever the | |
1170 associated process changes status for any reason, including signals | |
1171 (whether sent by Emacs or caused by the process's own actions) that | |
1172 terminate, stop, or continue the process. The process sentinel is also | |
1173 called if the process exits. The sentinel receives two arguments: the | |
1174 process for which the event occurred, and a string describing the type | |
1175 of event. | |
1176 | |
1177 The string describing the event looks like one of the following: | |
1178 | |
1179 @itemize @bullet | |
1180 @item | |
1181 @code{"finished\n"}. | |
1182 | |
1183 @item | |
1184 @code{"exited abnormally with code @var{exitcode}\n"}. | |
1185 | |
1186 @item | |
1187 @code{"@var{name-of-signal}\n"}. | |
1188 | |
1189 @item | |
1190 @code{"@var{name-of-signal} (core dumped)\n"}. | |
1191 @end itemize | |
1192 | |
1193 A sentinel runs only while Emacs is waiting (e.g., for terminal input, | |
1194 or for time to elapse, or for process output). This avoids the timing | |
1195 errors that could result from running them at random places in the | |
1196 middle of other Lisp programs. A program can wait, so that sentinels | |
9009 | 1197 will run, by calling @code{sit-for} or @code{sleep-for} |
1198 (@pxref{Waiting}), or @code{accept-process-output} (@pxref{Accepting | |
22138
d4ac295a98b3
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
21682
diff
changeset
|
1199 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
|
1200 reading input. |
6558 | 1201 |
1202 Quitting is normally inhibited within a sentinel---otherwise, the | |
1203 effect of typing @kbd{C-g} at command level or to quit a user command | |
1204 would be unpredictable. If you want to permit quitting inside a | |
1205 sentinel, bind @code{inhibit-quit} to @code{nil}. @xref{Quitting}. | |
1206 | |
1207 A sentinel that writes the output into the buffer of the process | |
12098 | 1208 should check whether the buffer is still alive. If it tries to insert |
6558 | 1209 into a dead buffer, it will get an error. If the buffer is dead, |
1210 @code{(buffer-name (process-buffer @var{process}))} returns @code{nil}. | |
1211 | |
12067 | 1212 If an error happens during execution of a sentinel, it is caught |
1213 automatically, so that it doesn't stop the execution of whatever | |
1214 programs was running when the sentinel was started. However, if | |
1215 @code{debug-on-error} is non-@code{nil}, the error-catching is turned | |
1216 off. This makes it possible to use the Lisp debugger to debug the | |
1217 sentinel. @xref{Debugger}. | |
1218 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1219 In earlier Emacs versions, every sentinel that did regular expression |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1220 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
|
1221 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
|
1222 it explicitly. @xref{Match Data}. |
12098 | 1223 |
6558 | 1224 @defun set-process-sentinel process sentinel |
1225 This function associates @var{sentinel} with @var{process}. If | |
1226 @var{sentinel} is @code{nil}, then the process will have no sentinel. | |
1227 The default behavior when there is no sentinel is to insert a message in | |
1228 the process's buffer when the process status changes. | |
1229 | |
1230 @smallexample | |
1231 @group | |
1232 (defun msg-me (process event) | |
1233 (princ | |
1234 (format "Process: %s had the event `%s'" process event))) | |
1235 (set-process-sentinel (get-process "shell") 'msg-me) | |
1236 @result{} msg-me | |
1237 @end group | |
1238 @group | |
1239 (kill-process (get-process "shell")) | |
1240 @print{} Process: #<process shell> had the event `killed' | |
1241 @result{} #<process shell> | |
1242 @end group | |
1243 @end smallexample | |
1244 @end defun | |
1245 | |
1246 @defun process-sentinel process | |
1247 This function returns the sentinel of @var{process}, or @code{nil} if it | |
1248 has none. | |
1249 @end defun | |
1250 | |
1251 @defun waiting-for-user-input-p | |
1252 While a sentinel or filter function is running, this function returns | |
1253 non-@code{nil} if Emacs was waiting for keyboard input from the user at | |
1254 the time the sentinel or filter function was called, @code{nil} if it | |
1255 was not. | |
1256 @end defun | |
1257 | |
1258 @node Transaction Queues | |
1259 @section Transaction Queues | |
1260 @cindex transaction queue | |
1261 | |
21007
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1262 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
|
1263 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
|
1264 queue communicating with a specified process. Then you can call |
66d807bdc5b4
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
17611
diff
changeset
|
1265 @code{tq-enqueue} to send a transaction. |
6558 | 1266 |
1267 @defun tq-create process | |
1268 This function creates and returns a transaction queue communicating with | |
1269 @var{process}. The argument @var{process} should be a subprocess | |
1270 capable of sending and receiving streams of bytes. It may be a child | |
9009 | 1271 process, or it may be a TCP connection to a server, possibly on another |
6558 | 1272 machine. |
1273 @end defun | |
1274 | |
1275 @defun tq-enqueue queue question regexp closure fn | |
1276 This function sends a transaction to queue @var{queue}. Specifying the | |
1277 queue has the effect of specifying the subprocess to talk to. | |
1278 | |
9009 | 1279 The argument @var{question} is the outgoing message that starts the |
6558 | 1280 transaction. The argument @var{fn} is the function to call when the |
1281 corresponding answer comes back; it is called with two arguments: | |
1282 @var{closure}, and the answer received. | |
1283 | |
1284 The argument @var{regexp} is a regular expression that should match the | |
1285 entire answer, but nothing less; that's how @code{tq-enqueue} determines | |
1286 where the answer ends. | |
1287 | |
1288 The return value of @code{tq-enqueue} itself is not meaningful. | |
1289 @end defun | |
1290 | |
1291 @defun tq-close queue | |
1292 Shut down transaction queue @var{queue}, waiting for all pending transactions | |
1293 to complete, and then terminate the connection or child process. | |
1294 @end defun | |
1295 | |
1296 Transaction queues are implemented by means of a filter function. | |
1297 @xref{Filter Functions}. | |
1298 | |
12098 | 1299 @node Network |
1300 @section Network Connections | |
1301 @cindex network connection | |
6558 | 1302 @cindex TCP |
1303 | |
12098 | 1304 Emacs Lisp programs can open TCP network connections to other processes on |
1305 the same machine or other machines. A network connection is handled by Lisp | |
6558 | 1306 much like a subprocess, and is represented by a process object. |
1307 However, the process you are communicating with is not a child of the | |
1308 Emacs process, so you can't kill it or send it signals. All you can do | |
1309 is send and receive data. @code{delete-process} closes the connection, | |
1310 but does not kill the process at the other end; that process must decide | |
1311 what to do about closure of the connection. | |
1312 | |
1313 You can distinguish process objects representing network connections | |
1314 from those representing subprocesses with the @code{process-status} | |
12098 | 1315 function. It always returns either @code{open} or @code{closed} for a |
1316 network connection, and it never returns either of those values for a | |
1317 real subprocess. @xref{Process Information}. | |
6558 | 1318 |
1319 @defun open-network-stream name buffer-or-name host service | |
1320 This function opens a TCP connection for a service to a host. It | |
1321 returns a process object to represent the connection. | |
1322 | |
1323 The @var{name} argument specifies the name for the process object. It | |
1324 is modified as necessary to make it unique. | |
1325 | |
1326 The @var{buffer-or-name} argument is the buffer to associate with the | |
1327 connection. Output from the connection is inserted in the buffer, | |
1328 unless you specify a filter function to handle the output. If | |
1329 @var{buffer-or-name} is @code{nil}, it means that the connection is not | |
1330 associated with any buffer. | |
1331 | |
1332 The arguments @var{host} and @var{service} specify where to connect to; | |
1333 @var{host} is the host name (a string), and @var{service} is the name of | |
1334 a defined network service (a string) or a port number (an integer). | |
1335 @end defun |