@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
-@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
+@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999
+@c Free Software Foundation, Inc.
@c See the file elisp.texi for copying conditions.
@setfilename ../info/processes
-@node Processes, System Interface, Abbrevs, Top
+@node Processes, Display, Abbrevs, Top
@chapter Processes
@cindex child process
@cindex parent process
@menu
* Subprocess Creation:: Functions that start subprocesses.
+* Shell Arguments:: Quoting an argument to pass it to a shell.
* Synchronous Processes:: Details of using synchronous subprocesses.
-* MS-DOS Subprocesses:: On MS-DOS, you must indicate text vs binary
- for data sent to and from a subprocess.
* Asynchronous Processes:: Starting up an asynchronous subprocess.
* Deleting Processes:: Eliminating an asynchronous subprocess.
* Process Information:: Accessing run-status and other attributes.
an asynchronous subprocess.
* Output from Processes:: Collecting output from an asynchronous subprocess.
* Sentinels:: Sentinels run when process run-status changes.
+* Query Before Exit:: Whether to query if exiting will kill a process.
* Transaction Queues:: Transaction-based communication with subprocesses.
* Network:: Opening network connections.
@end menu
create a synchronous process and do not return a process object
(@pxref{Synchronous Processes}).
- Synchronous and asynchronous processes are explained in following
+ Synchronous and asynchronous processes are explained in the following
sections. Since the three functions are all called in a similar
fashion, their common arguments are described here.
Each of the subprocess-creating functions has a @var{buffer-or-name}
argument which specifies where the standard output from the program will
-go. If @var{buffer-or-name} is @code{nil}, that says to discard the
-output unless a filter function handles it. (@xref{Filter Functions},
-and @ref{Read and Print}.) Normally, you should avoid having multiple
-processes send output to the same buffer because their output would be
-intermixed randomly.
+go. It should be a buffer or a buffer name; if it is a buffer name,
+that will create the buffer if it does not already exist. It can also
+be @code{nil}, which says to discard the output unless a filter function
+handles it. (@xref{Filter Functions}, and @ref{Read and Print}.)
+Normally, you should avoid having multiple processes send output to the
+same buffer because their output would be intermixed randomly.
@cindex program arguments
All three of the subprocess-creating functions have a @code{&rest}
argument, @var{args}. The @var{args} must all be strings, and they are
supplied to @var{program} as separate command line arguments. Wildcard
-characters and other shell constructs are not allowed in these strings,
-since they are passed directly to the specified program.
+characters and other shell constructs have no special meanings in these
+strings, since the whole strings are passed directly to the specified
+program.
@strong{Please note:} The argument @var{program} contains only the
name of the program; it may not contain any command-line arguments. You
@code{default-directory} (@pxref{File Name Expansion}).
@cindex environment variables, subprocesses
- The subprocess inherits its environment from Emacs; but you can
+ The subprocess inherits its environment from Emacs, but you can
specify overrides for it with @code{process-environment}. @xref{System
Environment}.
-@defvar exec-directory
+@defvar exec-directory
@pindex movemail
-The value of this variable is the name of a directory (a string) that
-contains programs that come with GNU Emacs, that are intended for Emacs
+The value of this variable is a string, the name of a directory that
+contains programs that come with GNU Emacs, programs intended for Emacs
to invoke. The program @code{movemail} is an example of such a program;
Rmail uses it to fetch new mail from an inbox.
@end defvar
file name.
@end defopt
+@node Shell Arguments
+@section Shell Arguments
+
+ Lisp programs sometimes need to run a shell and give it a command
+that contains file names that were specified by the user. These
+programs ought to be able to support any valid file name. But the shell
+gives special treatment to certain characters, and if these characters
+occur in the file name, they will confuse the shell. To handle these
+characters, use the function @code{shell-quote-argument}:
+
+@defun shell-quote-argument argument
+This function returns a string which represents, in shell syntax,
+an argument whose actual contents are @var{argument}. It should
+work reliably to concatenate the return value into a shell command
+and then pass it to a shell for execution.
+
+Precisely what this function does depends on your operating system. The
+function is designed to work with the syntax of your system's standard
+shell; if you use an unusual shell, you will need to redefine this
+function.
+
+@example
+;; @r{This example shows the behavior on GNU and Unix systems.}
+(shell-quote-argument "foo > bar")
+ @result{} "foo\\ \\>\\ bar"
+
+;; @r{This example shows the behavior on MS-DOS and MS-Windows systems.}
+(shell-quote-argument "foo > bar")
+ @result{} "\"foo > bar\""
+@end example
+
+Here's an example of using @code{shell-quote-argument} to construct
+a shell command:
+
+@example
+(concat "diff -c "
+ (shell-quote-argument oldfile)
+ " "
+ (shell-quote-argument newfile))
+@end example
+@end defun
+
@node Synchronous Processes
@section Creating a Synchronous Process
@cindex synchronous subprocess
After a @dfn{synchronous process} is created, Emacs waits for the
-process to terminate before continuing. Starting Dired is an example of
-this: it runs @code{ls} in a synchronous process, then modifies the
-output slightly. Because the process is synchronous, the entire
-directory listing arrives in the buffer before Emacs tries to do
-anything with it.
+process to terminate before continuing. Starting Dired on GNU or
+Unix@footnote{On other systems, Emacs uses a Lisp emulation of
+@code{ls}; see @ref{Contents of Directories}.} is an example of this: it
+runs @code{ls} in a synchronous process, then modifies the output
+slightly. Because the process is synchronous, the entire directory
+listing arrives in the buffer before Emacs tries to do anything with it.
While Emacs waits for the synchronous subprocess to terminate, the
user can quit by typing @kbd{C-g}. The first @kbd{C-g} tries to kill
the subprocess with a @code{SIGINT} signal; but it waits until the
subprocess actually terminates before quitting. If during that time the
user types another @kbd{C-g}, that kills the subprocess instantly with
-@code{SIGKILL} and quits immediately. @xref{Quitting}.
+@code{SIGKILL} and quits immediately (except on MS-DOS, where killing
+other processes doesn't work). @xref{Quitting}.
+
+ The synchronous subprocess functions return an indication of how the
+process terminated.
- The synchronous subprocess functions returned @code{nil} in version
-18. In version 19, they return an indication of how the process
-terminated.
+ The output from a synchronous subprocess is generally decoded using a
+coding system, much like text read from a file. The input sent to a
+subprocess by @code{call-process-region} is encoded using a coding
+system, much like text written into a file. @xref{Coding Systems}.
@defun call-process program &optional infile destination display &rest args
This function calls @var{program} in a separate process and waits for
it to finish.
The standard input for the process comes from file @var{infile} if
-@var{infile} is not @code{nil} and from @file{/dev/null} otherwise.
+@var{infile} is not @code{nil}, and from the null device otherwise.
The argument @var{destination} says where to put the process output.
Here are the possibilities:
standard output stream and the standard error stream of the process.
@item a string
-Find the buffer with that name, then insert the output in that buffer,
-before point.
+Insert the output in a buffer with that name, before point.
@item @code{t}
Insert the output in the current buffer, before point.
Discard the output.
@item 0
-Discard the output, and return immediately without waiting
+Discard the output, and return @code{nil} immediately without waiting
for the subprocess to finish.
In this case, the process is not truly synchronous, since it can run in
Emacs is essentially finished with the subprocess as soon as this
function returns.
-@item (@var{real-destination} @var{error-destination})
+MS-DOS doesn't support asynchronous subprocesses, so this option doesn't
+work there.
+
+@item @code{(@var{real-destination} @var{error-destination})}
Keep the standard output stream separate from the standard error stream;
deal with the ordinary output as specified by @var{real-destination},
and dispose of the error output according to @var{error-destination}.
-The value @code{nil} means discard it, @code{t} means mix it with the
-ordinary output, and a string specifies a file name to redirect error
-output into.
+If @var{error-destination} is @code{nil}, that means to discard the
+error output, @code{t} means mix it with the ordinary output, and a
+string specifies a file name to redirect error output into.
You can't directly specify a buffer to put the error output in; that is
too difficult to implement. But you can achieve this result by sending
@end table
If @var{display} is non-@code{nil}, then @code{call-process} redisplays
-the buffer as output is inserted. Otherwise the function does no
-redisplay, and the results become visible on the screen only when Emacs
-redisplays that buffer in the normal course of events.
+the buffer as output is inserted. (However, if the coding system chosen
+for decoding output is @code{undecided}, meaning deduce the encoding
+from the actual data, then redisplay sometimes cannot continue once
+non-@sc{ascii} characters are encountered. There are fundamental
+reasons why it is hard to fix this; see @ref{Output from Processes}.)
+
+Otherwise the function @code{call-process} does no redisplay, and the
+results become visible on the screen only when Emacs redisplays that
+buffer in the normal course of events.
The remaining arguments, @var{args}, are strings that specify command
line arguments for the program.
@smallexample
@group
(call-process "pwd" nil t)
- @result{} nil
+ @result{} 0
---------- Buffer: foo ----------
/usr/user/lewis/manual
@group
(call-process "grep" nil "bar" nil "lewis" "/etc/passwd")
- @result{} nil
+ @result{} 0
---------- Buffer: bar ----------
lewis:5LTsHm66CSWKg:398:21:Bil Lewis:/user/lewis:/bin/csh
@end group
@end smallexample
-The @code{insert-directory} function contains a good example of the use
-of @code{call-process}:
+Here is a good example of the use of @code{call-process}, which used to
+be found in the definition of @code{insert-directory}:
@smallexample
@group
-(call-process insert-directory-program nil t nil switches
+(call-process insert-directory-program nil t nil @var{switches}
(if full-directory-p
(concat (file-name-as-directory file) ".")
file))
@end defun
@defun call-process-region start end program &optional delete destination display &rest args
-This function sends the text between @var{start} to @var{end} as
+This function sends the text from @var{start} to @var{end} as
standard input to a process running @var{program}. It deletes the text
-sent if @var{delete} is non-@code{nil}; this is useful when @var{buffer}
-is @code{t}, to insert the output in the current buffer.
+sent if @var{delete} is non-@code{nil}; this is useful when
+@var{destination} is @code{t}, to insert the output in the current
+buffer in place of the input.
The arguments @var{destination} and @var{display} control what to do
with the output from the subprocess, and whether to update the display
as it comes in. For details, see the description of
@code{call-process}, above. If @var{destination} is the integer 0,
@code{call-process-region} discards the output and returns @code{nil}
-immediately, without waiting for the subprocess to finish.
+immediately, without waiting for the subprocess to finish (this only
+works if asynchronous subprocesses are supported).
The remaining arguments, @var{args}, are strings that specify command
line arguments for the program.
@group
(call-process-region 1 6 "cat" nil t)
- @result{} nil
+ @result{} 0
---------- Buffer: foo ----------
inputinput@point{}
@smallexample
@group
-(call-process-region
- start end
+(call-process-region
+ start end
shell-file-name ; @r{Name of program.}
nil ; @r{Do not delete region.}
buffer ; @r{Send output to @code{buffer}.}
@end smallexample
@end defun
-@node MS-DOS Subprocesses
-@section MS-DOS Subprocesses
-
- On MS-DOS, you must indicate whether the data going to and from
-a synchronous subprocess are text or binary. Text data requires
-translation between the end-of-line convention used within Emacs
-(a single newline character) and the convention used outside Emacs
-(the two-character sequence, @sc{crlf}).
-
- The variable @code{binary-process-input} applies to input sent to the
-subprocess, and @code{binary-process-output} applies to output received
-from it. A non-@code{nil} value means the data is non-text; @code{nil}
-means the data is text, and calls for conversion.
-
-@defvar binary-process-input
-If this variable is @code{nil}, convert newlines to @sc{crlf} sequences in
-the input to a synchronous subprocess.
-@end defvar
-
-@defvar binary-process-output
-If this variable is @code{nil}, convert @sc{crlf} sequences to newlines in
-the output from a synchronous subprocess.
-@end defvar
-
- @xref{Files and MS-DOS}, for related information.
+@defun shell-command-to-string command
+This function executes @var{command} (a string) as a shell command,
+then returns the command's output as a string.
+@end defun
@node Asynchronous Processes
@section Creating an Asynchronous Process
@cindex asynchronous subprocess
- After an @dfn{asynchronous process} is created, Emacs and the Lisp
-program both continue running immediately. The process may thereafter
-run in parallel with Emacs, and the two may communicate with each other
-using the functions described in following sections. Here we describe
-how to create an asynchronous process with @code{start-process}.
+ After an @dfn{asynchronous process} is created, Emacs and the subprocess
+both continue running immediately. The process thereafter runs
+in parallel with Emacs, and the two can communicate with each other
+using the functions described in the following sections. However,
+communication is only partially asynchronous: Emacs sends data to the
+process only when certain functions are called, and Emacs accepts data
+from the process only when Emacs is waiting for input or for a time
+delay.
+
+ Here we describe how to create an asynchronous process.
@defun start-process name buffer-or-name program &rest args
This function creates a new asynchronous subprocess and starts the
program @var{program} running in it. It returns a process object that
stands for the new subprocess in Lisp. The argument @var{name}
specifies the name for the process object; if a process with this name
-already exists, then @var{name} is modified (by adding @samp{<1>}, etc.)
-to be unique. The buffer @var{buffer-or-name} is the buffer to
+already exists, then @var{name} is modified (by appending @samp{<1>},
+etc.) to be unique. The buffer @var{buffer-or-name} is the buffer to
associate with the process.
The remaining arguments, @var{args}, are strings that specify command
This function is like @code{start-process} except that it uses a shell
to execute the specified command. The argument @var{command} is a shell
command name, and @var{command-args} are the arguments for the shell
-command.
+command. The variable @code{shell-file-name} specifies which shell to
+use.
+
+The point of running a program through the shell, rather than directly
+with @code{start-process}, is so that you can employ shell features such
+as wildcards in the arguments. It follows that if you include an
+arbitrary user-specified arguments in the command, you should quote it
+with @code{shell-quote-argument} first, so that any special shell
+characters do @emph{not} have their special shell meanings. @xref{Shell
+Arguments}.
@end defun
@defvar process-connection-type
@sc{pty}s are usually preferable for processes visible to the user, as
in Shell mode, because they allow job control (@kbd{C-c}, @kbd{C-z},
-etc.) to work between the process and its children whereas pipes do not.
-For subprocesses used for internal purposes by programs, it is often
-better to use a pipe, because they are more efficient. In addition, the
-total number of @sc{pty}s is limited on many systems and it is good not
-to waste them.
+etc.) to work between the process and its children, whereas pipes do
+not. For subprocesses used for internal purposes by programs, it is
+often better to use a pipe, because they are more efficient. In
+addition, the total number of @sc{pty}s is limited on many systems and
+it is good not to waste them.
-The value @code{process-connection-type} is used when
+The value of @code{process-connection-type} takes effect when
@code{start-process} is called. So you can specify how to communicate
with one subprocess by binding the variable around the call to
@code{start-process}.
@cindex deleting processes
@dfn{Deleting a process} disconnects Emacs immediately from the
-subprocess, and removes it from the list of active processes. It sends
-a signal to the subprocess to make the subprocess terminate, but this is
-not guaranteed to happen immediately. The process object itself
-continues to exist as long as other Lisp objects point to it. The
-process mark continues to point to the same place as before (usually
-into a buffer where output from the process was being inserted).
-
- You can delete a process explicitly at any time. Processes are
-deleted automatically after they terminate, but not necessarily right
-away. If you delete a terminated process explicitly before it is
-deleted automatically, no harm results.
-
-@defvar delete-exited-processes
+subprocess. Processes are deleted automatically after they terminate,
+but not necessarily right away. You can delete a process explicitly
+at any time. If you delete a terminated process explicitly before it
+is deleted automatically, no harm results. Deletion of a running
+process sends a signal to terminate it (and its child processes if
+any), and calls the process sentinel if it has one.
+
+ @code{get-buffer-process} and @code{process-list} do not remember a
+deleted process, but the process object itself continues to exist as
+long as other Lisp objects point to it. All the Lisp primitives that
+work on process objects accept deleted processes, but those that do
+I/O or send signals will report an error. The process mark continues
+to point to the same place as before, usually into a buffer where
+output from the process was being inserted.
+
+@defopt delete-exited-processes
This variable controls automatic deletion of processes that have
terminated (due to calling @code{exit} or to a signal). If it is
@code{nil}, then they continue to exist until the user runs
@code{list-processes}. Otherwise, they are deleted immediately after
they exit.
-@end defvar
+@end defopt
@defun delete-process name
-This function deletes the process associated with @var{name}, killing it
-with a @code{SIGHUP} signal. The argument @var{name} may be a process,
-the name of a process, a buffer, or the name of a buffer.
+This function deletes the process associated with @var{name}, killing
+it with a @code{SIGKILL} signal. The argument @var{name} may be a
+process, the name of a process, a buffer, or the name of a buffer.
+Calling @code{delete-process} on a running process terminates it,
+updates the process status, and runs the sentinel (if any) immediately.
+If the process has already terminated, calling @code{delete-process}
+has no effect on its status, or on the running of its sentinel (which
+will happen sooner or later).
@smallexample
@group
@end smallexample
@end defun
-@defun process-kill-without-query process
-This function declares that Emacs need not query the user if
-@var{process} is still running when Emacs is exited. The process will
-be deleted silently. The value is @code{t}.
-
-@smallexample
-@group
-(process-kill-without-query (get-process "shell"))
- @result{} t
-@end group
-@end smallexample
-@end defun
-
@node Process Information
@section Process Information
This function returns the name of @var{process}.
@end defun
+@defun process-contact process
+This function returns @code{t} for an ordinary child process, and
+@code{(@var{hostname} @var{service})} for a net connection
+(@pxref{Network}).
+@end defun
+
@defun process-status process-name
This function returns the status of @var{process-name} as a symbol.
The argument @var{process-name} must be a process, a buffer, a
For a network connection, @code{process-status} returns one of the symbols
@code{open} or @code{closed}. The latter means that the other side
closed the connection, or Emacs did @code{delete-process}.
-
-In earlier Emacs versions (prior to version 19), the status of a network
-connection was @code{run} if open, and @code{exit} if closed.
@end defun
@defun process-exit-status process
@ref{Asynchronous Processes}).
@end defun
+@defun process-coding-system process
+This function returns a cons cell describing the coding systems in use
+for decoding output from @var{process} and for encoding input to
+@var{process} (@pxref{Coding Systems}). The value has this form:
+
+@example
+(@var{coding-system-for-decoding} . @var{coding-system-for-encoding})
+@end example
+@end defun
+
+@defun set-process-coding-system process decoding-system encoding-system
+This function specifies the coding systems to use for subsequent output
+from and input to @var{process}. It will use @var{decoding-system} to
+decode subprocess output, and @var{encoding-system} to encode subprocess
+input.
+@end defun
+
@node Input to Processes
@section Sending Input to Processes
@cindex process input
the other characters, to force them through. For most programs,
these @sc{eof}s do no harm.
+ Subprocess input is normally encoded using a coding system before the
+subprocess receives it, much like text written into a file. You can use
+@code{set-process-coding-system} to specify which coding system to use
+(@pxref{Process Information}). Otherwise, the coding system comes from
+@code{coding-system-for-write}, if that is non-@code{nil}; or else from
+the defaulting mechanism (@pxref{Default Coding Systems}).
+
+ Sometimes the system is unable to accept input for that process,
+because the input buffer is full. When this happens, the send functions
+wait a short while, accepting output from subprocesses, and then try
+again. This gives the subprocess a chance to read more of its pending
+input and make space in the buffer. It also allows filters, sentinels
+and timers to run---so take account of that in writing your code.
+
@defun process-send-string process-name string
This function sends @var{process-name} the contents of @var{string} as
standard input. The argument @var{process-name} must be a process or
@end smallexample
@end defun
-@deffn Command process-send-region process-name start end
+@defun process-send-region process-name start end
This function sends the text in the region defined by @var{start} and
@var{end} as standard input to @var{process-name}, which is a process or
a process name. (If it is @code{nil}, the current buffer's process is
An error is signaled unless both @var{start} and @var{end} are
integers or markers that indicate positions in the current buffer. (It
is unimportant which number is larger.)
-@end deffn
+@end defun
@defun process-send-eof &optional process-name
This function makes @var{process-name} see an end-of-file in its
@end smallexample
@end defun
+@defun process-running-child-p process
+@tindex process-running-child-p process
+This function will tell you whether a subprocess has given control of
+its terminal to its own child process. The value is @code{t} if this is
+true, or if Emacs cannot tell; it is @code{nil} if Emacs can be certain
+that this is not so.
+@end defun
+
@node Signals to Processes
@section Sending Signals to Processes
@cindex process signals
signal @code{SIGTSTP}. Use @code{continue-process} to resume its
execution.
-On systems with job control, the ``stop character'' (usually @kbd{C-z})
-sends this signal (outside of Emacs). When @var{current-group} is
-non-@code{nil}, you can think of this function as ``typing @kbd{C-z}''
-on the terminal Emacs uses to communicate with the subprocess.
+Outside of Emacs, on systems with job control, the ``stop character''
+(usually @kbd{C-z}) normally sends this signal. When
+@var{current-group} is non-@code{nil}, you can think of this function as
+``typing @kbd{C-z}'' on the terminal Emacs uses to communicate with the
+subprocess.
@end defun
@defun continue-process &optional process-name current-group
the process has no buffer and no filter function, its output is
discarded.
+ When a subprocess terminates, Emacs reads any pending output,
+then stops reading output from that subprocess. Therefore, if the
+subprocess has children that are still live and still producing
+output, Emacs won't receive that output.
+
+ Output from a subprocess can arrive only while Emacs is waiting: when
+reading terminal input, in @code{sit-for} and @code{sleep-for}
+(@pxref{Waiting}), and in @code{accept-process-output} (@pxref{Accepting
+Output}). This minimizes the problem of timing errors that usually
+plague parallel programming. For example, you can safely create a
+process and only then specify its buffer or filter function; no output
+can arrive before you finish, if the code in between does not call any
+primitive that waits.
+
+ It is impossible to separate the standard output and standard error
+streams of the subprocess, because Emacs normally spawns the subprocess
+inside a pseudo-TTY, and a pseudo-TTY has only one output channel. If
+you want to keep the output to those streams separate, you should
+redirect one of them to a file--for example, by using an appropriate
+shell command.
+
+ Subprocess output is normally decoded using a coding system before the
+buffer or filter function receives it, much like text read from a file.
+You can use @code{set-process-coding-system} to specify which coding
+system to use (@pxref{Process Information}). Otherwise, the coding
+system comes from @code{coding-system-for-read}, if that is
+non-@code{nil}; or else from the defaulting mechanism (@pxref{Default
+Coding Systems}).
+
+ @strong{Warning:} Coding systems such as @code{undecided} which
+determine the coding system from the data do not work entirely reliably
+with asynchronous subprocess output. This is because Emacs has to
+process asynchronous subprocess output in batches, as it arrives. Emacs
+must try to detect the proper coding system from one batch at a time,
+and this does not always work. Therefore, if at all possible, use a
+coding system which determines both the character code conversion and
+the end of line conversion---that is, one like @code{latin-1-unix},
+rather than @code{undecided} or @code{latin-1}.
+
@menu
* Process Buffers:: If no filter, output is put in a buffer.
* Filter Functions:: Filter functions accept output from the process.
the end of the following section.
When the user is expected to enter input in the process buffer for
-transmission to the process, the process marker is useful for
-distinguishing the new input from previous output.
+transmission to the process, the process marker separates the new input
+from previous output.
@end defun
@defun set-process-buffer process buffer
@end defun
@defun get-buffer-process buffer-or-name
-This function returns the process associated with @var{buffer-or-name}.
-If there are several processes associated with it, then one is chosen.
-(Presently, the one chosen is the one most recently created.) It is
-usually a bad idea to have more than one process associated with the
-same buffer.
+This function returns a nondeleted process associated with the buffer
+specified by @var{buffer-or-name}. If there are several processes
+associated with it, this function chooses one (currently, the one most
+recently created, but don't count on that). Deletion of a process
+(see @code{delete-process}) makes it ineligible for this function to
+return.
+
+It is usually a bad idea to have more than one process associated with
+the same buffer.
@smallexample
@group
process buffer is used directly for output from the process only when
there is no filter.
- A filter function must accept two arguments: the associated process and
-a string, which is the output. The function is then free to do whatever it
-chooses with the output.
+ The filter function can only be called when Emacs is waiting for
+something, because process output arrives only at such times. Emacs
+waits when reading terminal input, in @code{sit-for} and
+@code{sleep-for} (@pxref{Waiting}), and in @code{accept-process-output}
+(@pxref{Accepting Output}).
- A filter function runs only while Emacs is waiting (e.g., for terminal
-input, or for time to elapse, or for process output). This avoids the
-timing errors that could result from running filters at random places in
-the middle of other Lisp programs. You may explicitly cause Emacs to
-wait, so that filter functions will run, by calling @code{sit-for} or
-@code{sleep-for} (@pxref{Waiting}), or @code{accept-process-output}
-(@pxref{Accepting Output}). Emacs is also waiting when the command loop
-is reading input.
+ A filter function must accept two arguments: the associated process
+and a string, which is output just received from it. The function is
+then free to do whatever it chooses with the output.
Quitting is normally inhibited within a filter function---otherwise,
the effect of typing @kbd{C-g} at command level or to quit a user
process's buffer, mimicking the actions of Emacs when there is no
filter. Such filter functions need to use @code{set-buffer} in order to
be sure to insert in that buffer. To avoid setting the current buffer
-semipermanently, these filter functions must use @code{unwind-protect}
-to make sure to restore the previous current buffer. They should also
-update the process marker, and in some cases update the value of point.
-Here is how to do these things:
+semipermanently, these filter functions must save and restore the
+current buffer. They should also update the process marker, and in some
+cases update the value of point. Here is how to do these things:
@smallexample
@group
(defun ordinary-insertion-filter (proc string)
- (let ((old-buffer (current-buffer)))
- (unwind-protect
- (let (moving)
- (set-buffer (process-buffer proc))
- (setq moving (= (point) (process-mark proc)))
+ (with-current-buffer (process-buffer proc)
+ (let ((moving (= (point) (process-mark proc))))
@end group
@group
- (save-excursion
- ;; @r{Insert the text, moving the process-marker.}
- (goto-char (process-mark proc))
- (insert string)
- (set-marker (process-mark proc) (point)))
- (if moving (goto-char (process-mark proc))))
- (set-buffer old-buffer))))
+ (save-excursion
+ ;; @r{Insert the text, advancing the process marker.}
+ (goto-char (process-mark proc))
+ (insert string)
+ (set-marker (process-mark proc) (point)))
+ (if moving (goto-char (process-mark proc))))))
@end group
@end smallexample
@noindent
-The reason to use an explicit @code{unwind-protect} rather than letting
-@code{save-excursion} restore the current buffer is so as to preserve
-the change in point made by @code{goto-char}.
+The reason to use @code{with-current-buffer}, rather than using
+@code{save-excursion} to save and restore the current buffer, is so as
+to preserve the change in point made by the second call to
+@code{goto-char}.
To make the filter force the process buffer to be visible whenever new
text arrives, insert the following line just before the
-@code{unwind-protect}:
+@code{with-current-buffer} construct:
@smallexample
(display-buffer (process-buffer proc))
@end smallexample
- To force point to move to the end of the new output no matter where
-it was previously, eliminate the variable @code{moving} and call
+ To force point to the end of the new output, no matter where it was
+previously, eliminate the variable @code{moving} and call
@code{goto-char} unconditionally.
- In earlier Emacs versions, every filter function that did regexp
-searching or matching had to explicitly save and restore the match data.
-Now Emacs does this automatically; filter functions never need to do it
-explicitly. @xref{Match Data}.
+ In earlier Emacs versions, every filter function that did regular
+expression searching or matching had to explicitly save and restore the
+match data. Now Emacs does this automatically for filter functions;
+they never need to do it explicitly. @xref{Match Data}.
A filter function that writes the output into the buffer of the
process should check whether the buffer is still alive. If it tries to
-insert into a dead buffer, it will get an error. If the buffer is dead,
-@code{(buffer-name (process-buffer @var{process}))} returns @code{nil}.
+insert into a dead buffer, it will get an error. The expression
+@code{(buffer-name (process-buffer @var{process}))} returns @code{nil}
+if the buffer is dead.
The output to the function may come in chunks of any size. A program
-that produces the same output twice in a row may send it as one batch
-of 200 characters one time, and five batches of 40 characters the next.
+that produces the same output twice in a row may send it as one batch of
+200 characters one time, and five batches of 40 characters the next. If
+the filter looks for certain text strings in the subprocess output, make
+sure to handle the case where one of these strings is split across two
+or more batches of output.
@defun set-process-filter process filter
This function gives @var{process} the filter function @var{filter}. If
;; @r{Insert input in the buffer specified by @code{my-shell-buffer}}
;; @r{and make sure that buffer is shown in some window.}
(defun my-process-filter (proc str)
- (let ((cur (selected-window))
- (pop-up-windows t))
- (pop-to-buffer my-shell-buffer)
+ (let ((cur (selected-window))
+ (pop-up-windows t))
+ (pop-to-buffer my-shell-buffer)
@end group
@group
- (goto-char (point-max))
- (insert str)
- (set-marker (process-mark proc) (point-max))
- (select-window cur)))
+ (goto-char (point-max))
+ (insert str)
+ (set-marker (process-mark proc) (point-max))
+ (select-window cur)))
@end group
@end smallexample
@end ignore
The argument @var{seconds} need not be an integer. If it is a floating
point number, this function waits for a fractional number of seconds.
Some systems support only a whole number of seconds; on these systems,
-@var{seconds} is rounded down. If the system doesn't support waiting
-fractions of a second, you get an error if you specify nonzero
-@var{millisec}.
+@var{seconds} is rounded down.
Not all operating systems support waiting periods other than multiples
of a second; on those that do not, you get an error if you specify
A @dfn{process sentinel} is a function that is called whenever the
associated process changes status for any reason, including signals
(whether sent by Emacs or caused by the process's own actions) that
-terminate, stop, or continue the process. The process sentinel is also
-called if the process exits. The sentinel receives two arguments: the
-process for which the event occurred, and a string describing the type
-of event.
+terminate, stop, or continue the process. The process sentinel is
+also called if the process exits. The sentinel receives two
+arguments: the process for which the event occurred, and a string
+describing the type of event.
The string describing the event looks like one of the following:
@itemize @bullet
-@item
+@item
@code{"finished\n"}.
@item
@code{"@var{name-of-signal} (core dumped)\n"}.
@end itemize
- A sentinel runs only while Emacs is waiting (e.g., for terminal input,
-or for time to elapse, or for process output). This avoids the timing
-errors that could result from running them at random places in the
-middle of other Lisp programs. A program can wait, so that sentinels
-will run, by calling @code{sit-for} or @code{sleep-for}
+ A sentinel runs only while Emacs is waiting (e.g., for terminal
+input, or for time to elapse, or for process output). This avoids the
+timing errors that could result from running them at random places in
+the middle of other Lisp programs. A program can wait, so that
+sentinels will run, by calling @code{sit-for} or @code{sleep-for}
(@pxref{Waiting}), or @code{accept-process-output} (@pxref{Accepting
-Output}). Emacs is also waiting when the command loop is reading input.
+Output}). Emacs also allows sentinels to run when the command loop is
+reading input. @code{delete-process} calls the sentinel when it
+terminates a running process.
+
+ Emacs does not keep a queue of multiple reasons to call the sentinel
+of one process; it records just the current status and the fact that
+there has been a change. Therefore two changes in status, coming in
+quick succession, can call the sentinel just once. However, process
+termination will always run the sentinel exactly once. This is
+because the process status can't change again after termination.
Quitting is normally inhibited within a sentinel---otherwise, the
effect of typing @kbd{C-g} at command level or to quit a user command
off. This makes it possible to use the Lisp debugger to debug the
sentinel. @xref{Debugger}.
- In earlier Emacs versions, every sentinel that did regexp searching or
-matching had to explicitly save and restore the match data. Now Emacs
-does this automatically; sentinels never need to do it explicitly.
-@xref{Match Data}.
+ While a sentinel is running, the process sentinel is temporarily
+set to @code{nil} so that the sentinel won't run recursively.
+For this reason it is not possible for a sentinel to specify
+a new sentinel.
+
+ In earlier Emacs versions, every sentinel that did regular expression
+searching or matching had to explicitly save and restore the match data.
+Now Emacs does this automatically for sentinels; they never need to do
+it explicitly. @xref{Match Data}.
@defun set-process-sentinel process sentinel
This function associates @var{sentinel} with @var{process}. If
The default behavior when there is no sentinel is to insert a message in
the process's buffer when the process status changes.
+Changes in process sentinel take effect immediately---if the sentinel
+is slated to be run but has not been called yet, and you specify a new
+sentinel, the eventual call to the sentinel will use the new one.
+
@smallexample
@group
(defun msg-me (process event)
was not.
@end defun
+@node Query Before Exit
+@section Querying Before Exit
+
+ When Emacs exits, it terminates all its subprocesses by sending them
+the @code{SIGHUP} signal. Because some subprocesses are doing
+valuable work, Emacs normally asks the user to confirm that it is ok
+to terminate them. Each process has a query flag which, if
+non-@code{nil}, says that Emacs should ask for confirmation before
+exiting and thus killing that process. The default for the query flag
+is @code{t}, meaning @emph{do} query.
+
+@tindex process-query-on-exit-flag
+@defun process-query-on-exit-flag process
+This returns the query flag of @var{process}.
+@end defun
+
+@tindex set-process-query-on-exit-flag
+@defun set-process-query-on-exit-flag process flag
+This function sets the query flag of @var{process} to @var{flag}. It
+returns @var{flag}.
+
+@smallexample
+@group
+;; @r{Don't query about the shell process}
+(set-process-query-on-exit-flag (get-process "shell") nil)
+ @result{} t
+@end group
+@end smallexample
+@end defun
+
+@defun process-kill-without-query process &optional do-query
+This function clears the query flag of @var{process}, so that
+Emacs will not query the user on account of that process.
+
+Actually, the function does more than that: it returns the old value of
+the process's query flag, and sets the query flag to @var{do-query}.
+Please don't use this function to do those things any more---please
+use the newer, cleaner functions @code{process-query-on-exit-flag} and
+@code{set-process-query-on-exit-flag} in all but the simplest cases.
+The only way you should use @code{process-kill-without-query} nowadays
+is like this:
+
+@smallexample
+@group
+;; @r{Don't query about the shell process}
+(process-kill-without-query (get-process "shell"))
+@end group
+@end smallexample
+@end defun
+
@node Transaction Queues
@section Transaction Queues
@cindex transaction queue
-You can use a @dfn{transaction queue} for more convenient communication
-with subprocesses using transactions. First use @code{tq-create} to
-create a transaction queue communicating with a specified process. Then
-you can call @code{tq-enqueue} to send a transaction.
+You can use a @dfn{transaction queue} to communicate with a subprocess
+using transactions. First use @code{tq-create} to create a transaction
+queue communicating with a specified process. Then you can call
+@code{tq-enqueue} to send a transaction.
@defun tq-create process
This function creates and returns a transaction queue communicating with
corresponding answer comes back; it is called with two arguments:
@var{closure}, and the answer received.
-The argument @var{regexp} is a regular expression that should match the
-entire answer, but nothing less; that's how @code{tq-enqueue} determines
-where the answer ends.
+The argument @var{regexp} is a regular expression that should match
+text at the end of the entire answer, but nothing before; that's how
+@code{tq-enqueue} determines where the answer ends.
The return value of @code{tq-enqueue} itself is not meaningful.
@end defun