annotate lisp/process.el @ 5750:66d2f63df75f

Correct some spelling and formatting in behavior.el. Mentioned in tracker issue 826, the third thing mentioned there (the file name at the bottom of the file) had already been fixed. lisp/ChangeLog addition: 2013-08-05 Aidan Kehoe <kehoea@parhasard.net> * behavior.el: (override-behavior): Correct some spelling and formatting here, thank you Steven Mitchell in tracker issue 826.
author Aidan Kehoe <kehoea@parhasard.net>
date Mon, 05 Aug 2013 10:05:32 +0100
parents f6af091ac654
children bbe4146603db
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 ;;; process.el --- commands for subprocesses; split out of simple.el
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2
5687
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
3 ;; Copyright (C) 1985-7, 1993,4, 1997, 2011, 2012 Free Software Foundation, Inc.
853
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
4 ;; Copyright (C) 1995, 2000, 2001, 2002 Ben Wing.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 ;; Author: Ben Wing
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 ;; Maintainer: XEmacs Development Team
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 ;; Keywords: internal, processes, dumped
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 ;; This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4611
diff changeset
12 ;; XEmacs is free software: you can redistribute it and/or modify it
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4611
diff changeset
13 ;; under the terms of the GNU General Public License as published by the
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4611
diff changeset
14 ;; Free Software Foundation, either version 3 of the License, or (at your
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4611
diff changeset
15 ;; option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4611
diff changeset
17 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4611
diff changeset
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4611
diff changeset
19 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4611
diff changeset
20 ;; for more details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 4611
diff changeset
23 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
25 ;;; Synched up with: FSF 19.30, except for setenv/getenv (synched with FSF
4327
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
26 ;;; 21.2.1).
5533
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
27 ;;; shell-command and shell-command-on-region synced with FSF 23.3.1.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
29 ;;; Authorship:
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
30
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
31 ;; Created 1995 by Ben Wing during Mule work -- some commands split out
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
32 ;; of simple.el and wrappers of *-internal functions created so they could
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
33 ;; be redefined in a Mule world.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
34 ;; Lisp definition of call-process-internal added Mar. 2000 by Ben Wing.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
35
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 ;;; Commentary:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 ;; This file is dumped with XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 ;;; Code:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 (defgroup processes nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 "Process, subshell, compilation, and job control support."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 :group 'external
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 :group 'development)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 (defgroup processes-basics nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 "Basic stuff dealing with processes."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 :group 'processes)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 (defgroup execute nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 "Executing external commands."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 :group 'processes)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55
611
38db05db9cb5 [xemacs-hg @ 2001-06-08 12:21:09 by ben]
ben
parents: 444
diff changeset
56 ;; This may be changed to "/c" in win32-native.el.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 (defvar shell-command-switch "-c"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 "Switch used to have the shell execute its command line argument.")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60
5533
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
61 (defvar shell-command-default-error-buffer nil
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
62 "*Buffer name for `shell-command' and `shell-command-on-region' error output.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
63 This buffer is used when `shell-command' or `shell-command-on-region'
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
64 is run interactively. A value of nil means that output to stderr and
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
65 stdout will be intermixed in the output stream.")
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
66
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 (defun start-process-shell-command (name buffer &rest args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 "Start a program in a subprocess. Return the process object for it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 NAME is name for process. It is modified if necessary to make it unique.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 BUFFER is the buffer or (buffer-name) to associate with the process.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 Process output goes at end of that buffer, unless you specify
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 an output stream or filter function to handle the output.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 BUFFER may be also nil, meaning that this process is not associated
2473
35aac3cef6af [xemacs-hg @ 2005-01-09 09:44:43 by adrian]
adrian
parents: 1189
diff changeset
74 with any buffer.
35aac3cef6af [xemacs-hg @ 2005-01-09 09:44:43 by adrian]
adrian
parents: 1189
diff changeset
75 Variables `shell-file-name' and `shell-command-switch' are used to
35aac3cef6af [xemacs-hg @ 2005-01-09 09:44:43 by adrian]
adrian
parents: 1189
diff changeset
76 start the process.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 Remaining arguments are the arguments for the command.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 Wildcards and redirection are handled as usual in the shell."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 ;; We used to use `exec' to replace the shell with the command,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 ;; but that failed to handle (...) and semicolon, etc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 (start-process name buffer shell-file-name shell-command-switch
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 (mapconcat #'identity args " ")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83
5687
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
84 (defun call-process-shell-command (command &optional infile buffer display
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
85 &rest args)
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
86 "Execute the shell command COMMAND synchronously in separate process.
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
87 The remaining arguments are optional.
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
88 The program's input comes from file INFILE (nil means `/dev/null').
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
89 Insert output in BUFFER before point; t means current buffer;
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
90 nil for BUFFER means discard it; 0 means discard and don't wait.
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
91 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
92 REAL-BUFFER says what to do with standard output, as above,
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
93 while STDERR-FILE says what to do with standard error in the child.
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
94 STDERR-FILE may be nil (discard standard error output),
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
95 t (mix it with ordinary output), or a file name string.
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
96
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
97 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
98 Remaining arguments are strings passed as additional arguments for COMMAND.
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
99 Wildcards and redirection are handled as usual in the shell.
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
100
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
101 If BUFFER is 0, `call-process-shell-command' returns immediately with value nil.
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
102 Otherwise it waits for COMMAND to terminate and returns a numeric exit
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
103 status or a signal description string.
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
104 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again."
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
105 ;; We used to use `exec' to replace the shell with the command,
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
106 ;; but that failed to handle (...) and semicolon, etc.
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
107 (call-process shell-file-name
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
108 infile buffer display
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
109 shell-command-switch
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
110 (mapconcat 'identity (cons command args) " ")))
965a9ddc915a Introduce call-process-shell-command from GNU.
Mats Lidell <matsl@xemacs.org>
parents: 5652
diff changeset
111
853
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
112 (defun call-process-internal (program &optional infile buffer display
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
113 &rest args)
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
114 "Internal function to call PROGRAM synchronously in separate process.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
115 Lisp callers should use `call-process' or `call-process-region'.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
116
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
117 The program's input comes from file INFILE (nil means `/dev/null').
853
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
118 XEmacs feature: INFILE can also be a list of (BUFFER [START [END]]), i.e.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
119 a list of one to three elements, consisting of a buffer and optionally
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
120 a start position or start and end position. In this case, input comes
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
121 from the buffer, starting from START (defaults to the beginning of the
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
122 buffer) and ending at END (defaults to the end of the buffer).
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
123
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
124 Insert output in BUFFER before point; t means current buffer;
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
125 nil for BUFFER means discard it; 0 means discard and don't wait.
853
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
126 If BUFFER is a string, then find or create a buffer with that name,
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
127 then insert the output in that buffer, before point.
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
128 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
129 REAL-BUFFER says what to do with standard output, as above,
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
130 while STDERR-FILE says what to do with standard error in the child.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
131 STDERR-FILE may be nil (discard standard error output),
853
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
132 t (mix it with ordinary output), a file name string, or (XEmacs feature)
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
133 a buffer object. If STDERR-FILE is a buffer object (but not the name of
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
134 a buffer, since that would be interpreted as a file), the standard error
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
135 output will be inserted into the buffer before point.
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
136
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
137 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
138 Remaining arguments are strings passed as command arguments to PROGRAM.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
139
853
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
140 If BUFFER is 0, returns immediately with value nil.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
141 Otherwise waits for PROGRAM to terminate and returns a numeric exit status
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
142 or a signal description string. If you quit, the process is first killed
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
143 with SIGINT, then with SIGKILL if you quit again before the process exits.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
144
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
145 Coding systems for the process are the same as for `start-process-internal'."
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
146 (let (proc inbuf errbuf kill-inbuf kill-errbuf no-wait start end)
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
147 ;; first set up an unwind-protect to clean everything up. this will:
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
148 ;;
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
149 ;; -- kill the process. (when we're not waiting for it to finish, we
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
150 ;; set PROC to nil when we're ready to exit so this doesn't happen --
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
151 ;; if we're interrupted before we're ready to exit, we should still
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
152 ;; kill the process)
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
153 ;; -- kill temporary buffers created to handle I/O to or from a file.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
154 ;; KILL-INBUF/KILL-ERRBUF tell us if we should do so.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
155 ;;
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
156 ;; note that we need to be *very* careful in this code to handle C-g
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
157 ;; at any point.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
158 (unwind-protect
5738
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
159 (labels
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
160 ((process-synchronize-point (proc)
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
161 ;; Set the point(s) in buffer and stderr-buffer according to
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
162 ;; the process mark.
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
163 ;;
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
164 ;; We need this because the documentation says to insert
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
165 ;; *BEFORE* point, but we end up inserting after because only
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
166 ;; the process mark moves forward, not point. We synchronize
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
167 ;; after every place output might happen, in sentinels, and
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
168 ;; in an unwind-protect, to make *SURE* that point is
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
169 ;; correct. (We could do this more easily and perhaps more
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
170 ;; safely using a process filter, but that would create a LOT
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
171 ;; of garbage since all the data would get sent in strings.)
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
172 ;; We make this a label, not an flet, due to dynamic binding
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
173 ;; problems -- the flet may not still be in scope when the
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
174 ;; sentinel is called.
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
175 (let ((pb (process-buffer proc))
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
176 (pm (process-mark proc)))
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
177 (if (and pb (buffer-live-p pb) (marker-buffer pm))
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
178 (goto-char pm pb))
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
179 (if (process-has-separate-stderr-p proc)
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
180 (let ((pseb (process-stderr-buffer proc))
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
181 (psem (process-stderr-mark proc)))
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
182 (if (and pseb (not (eq pb pseb))
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
183 (buffer-live-p pseb)
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
184 (marker-buffer psem))
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
185 (goto-char psem pseb)))))))
853
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
186 ;; first handle INFILE.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
187 (cond ((stringp infile)
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
188 (setq infile (expand-file-name infile))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
189 (setq kill-inbuf t)
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
190 (setq inbuf (generate-new-buffer "*call-process*"))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
191 ;; transfer the exact contents of the file to the process.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
192 ;; we do that by reading in and writing out in
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
193 ;; binary. #### is this even correct? should we be doing
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
194 ;; the same thing with stderr? if so we'd need a way of
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
195 ;; controlling the stderr coding system separate from
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
196 ;; everything else.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
197 (with-current-buffer inbuf
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
198 ;; Make sure this works with jka-compr
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
199 (let ((file-name-handler-alist nil))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
200 (insert-file-contents-internal infile nil nil nil nil
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
201 'binary))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
202 (setq start (point-min) end (point-max))))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
203 ((consp infile)
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
204 (setq inbuf (get-buffer (car infile)))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
205 (setq start (or (nth 1 infile) (point-min inbuf)))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
206 (setq end (or (nth 2 infile) (point-max inbuf))))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
207 ((null infile) nil)
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
208 (t
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
209 (error 'wrong-type-argument
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
210 "Must be filename or (BUFFER [START [END]])"
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
211 infile)))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
212 ;; now handle BUFFER
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
213 (let ((stderr (if (consp buffer) (second buffer) t)))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
214 (if (consp buffer) (setq buffer (car buffer)))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
215 (setq buffer
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
216 (cond ((null buffer) nil)
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
217 ((eq buffer t) (current-buffer))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
218 ;; use integerp for compatibility with existing
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
219 ;; call-process rmsism.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
220 ((integerp buffer) (setq no-wait t) nil)
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
221 (t (get-buffer-create buffer))))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
222 (when (and stderr (not (eq t stderr)))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
223 ;; both ERRBUF and STDERR being non-nil indicates to the
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
224 ;; code below that STDERR is a file and we should write
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
225 ;; ERRBUF to it; so clear out STDERR if we don't want this.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
226 (if (bufferp stderr) (setq errbuf stderr stderr nil)
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
227 (setq stderr (expand-file-name stderr))
853
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
228 (setq kill-errbuf t)
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
229 (setq errbuf (generate-new-buffer "*call-process*"))))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
230 ;; now start process. using a pty causes all sorts of
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
231 ;; weirdness, at least under cygwin, when there's input. #### i
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
232 ;; don't know what's going wrong and whether it's a cygwin-only
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
233 ;; problem. suffice to say that there were NO pty connections
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
234 ;; in the old version.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
235 (let ((process-connection-type nil))
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
236 (setq proc
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
237 (apply 'start-process-internal "*call-process*"
853
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
238 (if (eq t stderr) buffer (list buffer errbuf))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
239 program args)))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
240 ;; see comment above where the data was read from the file.
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
241 (if kill-inbuf
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
242 (set-process-output-coding-system proc 'binary))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
243 ;; point mark/stderr-mark at the right place (by default it's
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
244 ;; end of buffer).
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
245 (if buffer
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
246 (set-marker (process-mark proc) (point buffer) buffer))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
247 (if errbuf
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
248 (set-marker (process-stderr-mark proc) (point errbuf) errbuf))
859
84762348c6f9 [xemacs-hg @ 2002-06-01 08:06:46 by ben]
ben
parents: 853
diff changeset
249 ;; now do I/O, very carefully! the unwind-protect makes sure
84762348c6f9 [xemacs-hg @ 2002-06-01 08:06:46 by ben]
ben
parents: 853
diff changeset
250 ;; to clear out the sentinel, since it does a `throw', which
84762348c6f9 [xemacs-hg @ 2002-06-01 08:06:46 by ben]
ben
parents: 853
diff changeset
251 ;; would have no catch (or writes to a file -- we only want
84762348c6f9 [xemacs-hg @ 2002-06-01 08:06:46 by ben]
ben
parents: 853
diff changeset
252 ;; this on normal exit)
862
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
253 (unwind-protect
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
254 ;; if not NO-WAIT, set a sentinel to return the exit
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
255 ;; status. it will throw to this catch so we can exit
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
256 ;; properly.
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
257 (catch 'call-process-done
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
258 (set-process-sentinel
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
259 proc
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
260 (cond
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
261 ((and no-wait errbuf stderr)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
262 ;; we're trying really really hard to emulate
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
263 ;; the old call-process, which would save the
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
264 ;; stderr to a file even if discarding output. so
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
265 ;; we set a sentinel to save the output when
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
266 ;; we finish.
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
267 ;;
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
268 ;; #### not clear if we should be doing this.
5738
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
269 (apply-partially
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
270 #'(lambda (errbuf stderr proc status)
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
271 (set-process-sentinel proc nil)
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
272 (process-synchronize-point proc)
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
273 (with-current-buffer errbuf
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
274 (write-region-internal
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
275 1 (1+ (buffer-size))
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
276 stderr
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
277 nil 'major-rms-kludge-city nil
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
278 coding-system-for-write))
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
279 (kill-buffer errbuf))
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
280 ;; Close around these two variables, the lambda may be
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
281 ;; called outside this enclosing unwind-protect.
f6af091ac654 Use new language features in #'call-process-internal now they're available.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5687
diff changeset
282 errbuf stderr))
862
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
283 (no-wait nil)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
284 (t
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
285 ;; normal sentinel: maybe write out stderr and return
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
286 ;; status.
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
287 #'(lambda (proc status)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
288 (process-synchronize-point proc)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
289 (when (and errbuf stderr)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
290 (with-current-buffer errbuf
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
291 (write-region-internal
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
292 1 (1+ (buffer-size)) stderr
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
293 nil 'major-rms-kludge-city nil
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
294 coding-system-for-write)))
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
295 (cond ((eq 'exit (process-status proc))
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
296 (set-process-sentinel proc nil)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
297 (throw 'call-process-done
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
298 (process-exit-status proc)))
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
299 ((eq 'signal (process-status proc))
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
300 (set-process-sentinel proc nil)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
301 (throw 'call-process-done status)))))))
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
302 (if (not no-wait)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
303 ;; we're waiting. send the input and loop forever,
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
304 ;; handling process output and maybe redisplaying.
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
305 ;; exit happens through the sentinel or C-g. if
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
306 ;; C-g, send SIGINT the first time, EOF if not
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
307 ;; already done so (might make the process exit),
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
308 ;; and keep waiting. Another C-g will exit the
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
309 ;; whole function, and the unwind-protect will
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
310 ;; kill the process. (Hence the documented semantics
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
311 ;; of SIGINT/SIGKILL.)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
312 (let (eof-sent)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
313 (condition-case nil
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
314 (progn
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
315 (when inbuf
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
316 (process-send-region proc start end inbuf))
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
317 (process-send-eof proc)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
318 (setq eof-sent t)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
319 (while t
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
320 (accept-process-output proc)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
321 (process-synchronize-point proc)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
322 (if display (sit-for 0))))
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
323 (quit
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
324 (process-send-signal 'SIGINT proc)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
325 (unless eof-sent
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
326 (process-send-eof proc))
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
327 (while t
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
328 (accept-process-output proc)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
329 (process-synchronize-point proc)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
330 (if display (sit-for 0))))))
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
331 ;; discard and no wait: send the input, set PROC
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
332 ;; and ERRBUF to nil so that the unwind-protect
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
333 ;; forms don't erase the sentinel, kill the process,
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
334 ;; or kill ERRBUF (the sentinel does that), and exit.
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
335 (when inbuf
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
336 (process-send-region proc start end inbuf))
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
337 (process-send-eof proc)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
338 (setq errbuf nil)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
339 (setq proc nil)))
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
340 ;; inner unwind-protect, once we're ready to do I/O.
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
341 (when proc
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
342 (set-process-sentinel proc nil)
278c743f1578 [xemacs-hg @ 2002-06-03 12:23:49 by ben]
ben
parents: 859
diff changeset
343 (process-synchronize-point proc)))))
859
84762348c6f9 [xemacs-hg @ 2002-06-01 08:06:46 by ben]
ben
parents: 853
diff changeset
344 ;; outer unwind-protect forms, to make sure we always clean up.
853
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
345 (if (and inbuf kill-inbuf) (kill-buffer inbuf))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
346 (if (and errbuf kill-errbuf) (kill-buffer errbuf))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
347 (condition-case nil
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
348 (if (and proc (process-live-p proc)) (kill-process proc))
2b6fa2618f76 [xemacs-hg @ 2002-05-28 08:44:22 by ben]
ben
parents: 814
diff changeset
349 (error nil)))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351
5533
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
352 (defun shell-command (command &optional output-buffer error-buffer)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 "Execute string COMMAND in inferior shell; display output, if any.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354
5533
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
355 If COMMAND ends in ampersand, execute it asynchronously. The command
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
356 is executed using the background package. See `background' for
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
357 details.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 Otherwise, COMMAND is executed synchronously. The output appears in the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 buffer `*Shell Command Output*'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 If the output is one line, it is displayed in the echo area *as well*,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 but it is nonetheless available in buffer `*Shell Command Output*',
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 even though that buffer is not automatically displayed.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 If there is no output, or if output is inserted in the current buffer,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 then `*Shell Command Output*' is deleted.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 The optional second argument OUTPUT-BUFFER, if non-nil,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 says to put the output in some other buffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 If OUTPUT-BUFFER is not a buffer and not nil,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 insert output in current buffer. (This cannot be done asynchronously.)
5533
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
372 In either case, the output is inserted after point (leaving mark after it).
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
373
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
374 If the optional third argument ERROR-BUFFER is non-nil, it is a buffer
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
375 or buffer name to which to direct the command's standard error output.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
376 If it is nil, error output is mingled with regular output. In an
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
377 interactive call, the variable `shell-command-default-error-buffer'
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
378 specifies the value of ERROR-BUFFER."
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 (interactive (list (read-shell-command "Shell command: ")
5533
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
380 current-prefix-arg
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
381 shell-command-default-error-buffer))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 (if (and output-buffer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 (not (or (bufferp output-buffer) (stringp output-buffer))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 (progn (barf-if-buffer-read-only)
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 442
diff changeset
385 (push-mark nil (not (interactive-p)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 ;; We do not use -f for csh; we will not support broken use of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 ;; .cshrcs. Even the BSD csh manual says to use
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 ;; "if ($?prompt) exit" before things which are not useful
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 ;; non-interactively. Besides, if someone wants their other
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 ;; aliases for shell commands then they can still have them.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 (call-process shell-file-name nil t nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 shell-command-switch command)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 (exchange-point-and-mark t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 ;; Preserve the match data in case called from a program.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 (save-match-data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396 (if (string-match "[ \t]*&[ \t]*$" command)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 ;; Command ending with ampersand means asynchronous.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 (progn
776
79940b592197 [xemacs-hg @ 2002-03-15 07:43:14 by ben]
ben
parents: 771
diff changeset
399 (if-fboundp 'background
3491
81975997fb1a [xemacs-hg @ 2006-07-06 17:07:40 by james]
james
parents: 2473
diff changeset
400 (background (substring command 0 (match-beginning 0))
81975997fb1a [xemacs-hg @ 2006-07-06 17:07:40 by james]
james
parents: 2473
diff changeset
401 output-buffer)
776
79940b592197 [xemacs-hg @ 2002-03-15 07:43:14 by ben]
ben
parents: 771
diff changeset
402 (error
79940b592197 [xemacs-hg @ 2002-03-15 07:43:14 by ben]
ben
parents: 771
diff changeset
403 'unimplemented
79940b592197 [xemacs-hg @ 2002-03-15 07:43:14 by ben]
ben
parents: 771
diff changeset
404 "backgrounding a shell command requires package `background'")))
79940b592197 [xemacs-hg @ 2002-03-15 07:43:14 by ben]
ben
parents: 771
diff changeset
405
5533
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
406 (shell-command-on-region (point) (point) command output-buffer nil error-buffer)))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408 ;; We have a sentinel to prevent insertion of a termination message
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 ;; in the buffer itself.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 (defun shell-command-sentinel (process signal)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 (if (memq (process-status process) '(exit signal))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 (message "%s: %s."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 (car (cdr (cdr (process-command process))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 (substring signal 0 -1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416 (defun shell-command-on-region (start end command
5533
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
417 &optional output-buffer replace
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
418 error-buffer display-error-buffer)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 "Execute string COMMAND in inferior shell with region as input.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 Normally display output (if any) in temp buffer `*Shell Command Output*';
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 Prefix arg means replace the region with it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, REPLACE.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 If REPLACE is non-nil, that means insert the output
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 in place of text from START to END, putting point and mark around it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 If the output is one line, it is displayed in the echo area,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 but it is nonetheless available in buffer `*Shell Command Output*'
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 even though that buffer is not automatically displayed.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 If there is no output, or if output is inserted in the current buffer,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 then `*Shell Command Output*' is deleted.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 If the optional fourth argument OUTPUT-BUFFER is non-nil,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 that says to put the output in some other buffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 If OUTPUT-BUFFER is not a buffer and not nil,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 insert output in the current buffer.
5533
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
438 In either case, the output is inserted after point (leaving mark after it).
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
439
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
440 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
441 or buffer name to which to direct the command's standard error output.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
442 If it is nil, error output is mingled with regular output.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
443 If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
444 were any errors. (This is always t, interactively.)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
445 In an interactive call, the variable `shell-command-default-error-buffer'
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
446 specifies the value of ERROR-BUFFER."
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 (interactive (let ((string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 ;; Do this before calling region-beginning
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 ;; and region-end, in case subprocess output
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 ;; relocates them while we are in the minibuffer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 (read-shell-command "Shell command on region: ")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 ;; call-interactively recognizes region-beginning and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 ;; region-end specially, leaving them in the history.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 (list (region-beginning) (region-end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 string
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 current-prefix-arg
5533
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
457 current-prefix-arg
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
458 shell-command-default-error-buffer
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
459 t)))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
460 (let ((error-file
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
461 (if error-buffer
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
462 (make-temp-file
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
463 (expand-file-name "scor"
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
464 (or small-temporary-file-directory
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
465 (temp-directory))))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
466 nil))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
467 (exit-status nil))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
468 (if (or replace
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
469 (and output-buffer
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
470 (not (or (bufferp output-buffer) (stringp output-buffer)))))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
471 ;; Replace specified region with output from command.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
472 (let ((swap (and replace (< start end))))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
473 ;; Don't muck with mark unless REPLACE says we should.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
474 (goto-char start)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
475 (and replace (push-mark))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
476 (setq exit-status (call-process-region start end shell-file-name t
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
477 (if error-file
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
478 (list t error-file)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
479 t)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
480 nil shell-command-switch command))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
481 (let ((shell-buffer (get-buffer "*Shell Command Output*")))
5555
a39cd9dc92ba Correct a typo from Mats' merge, process.el, thank you the byte-compiler
Aidan Kehoe <kehoea@parhasard.net>
parents: 5533
diff changeset
482 (and shell-buffer (not (eq shell-buffer (current-buffer)))
5533
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
483 (kill-buffer shell-buffer)))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
484 ;; Don't muck with mark unless REPLACE says we should.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
485 (and replace swap (exchange-point-and-mark t)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 ;; No prefix argument: put the output in a temp buffer,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 ;; replacing its entire contents.
5533
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
488 (let ((buffer (get-buffer-create
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
489 (or output-buffer "*Shell Command Output*")))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
490 (directory default-directory))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
491 (unwind-protect
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
492 (if (eq buffer (current-buffer))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
493 ;; If the input is the same buffer as the output,
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
494 ;; delete everything but the specified region,
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
495 ;; then replace that region with the output.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
496 (progn (setq buffer-read-only nil)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
497 (delete-region (max start end) (point-max))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
498 (delete-region (point-min) (min start end))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
499 (setq exit-status
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
500 (call-process-region (point-min) (point-max)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
501 shell-file-name t
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
502 (if error-file
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
503 (list t error-file)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
504 t)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
505 nil shell-command-switch
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
506 command)))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
507 ;; Clear the output buffer,
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
508 ;; then run the command with output there.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
509 (save-excursion
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
510 (set-buffer buffer)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
511 (setq buffer-read-only nil)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
512 ;; XEmacs change
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
513 (setq default-directory directory)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
514 (erase-buffer))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
515 (setq exit-status
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
516 (call-process-region start end shell-file-name
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
517 nil
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
518 (if error-file
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
519 (list buffer error-file)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
520 buffer)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
521 nil shell-command-switch command)))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
522
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
523 ;; Report the output.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
524 (with-current-buffer buffer
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
525 (setq modeline-process
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
526 (cond ((null exit-status)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
527 " - Error")
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
528 ((stringp exit-status)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
529 (format " - Signal [%s]" exit-status))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
530 ((not (equal 0 exit-status))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
531 (format " - Exit [%d]" exit-status)))))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
532 (if (with-current-buffer buffer (> (point-max) (point-min)))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
533 ;; There's some output, display it
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
534 (let ((lines (save-excursion
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
535 (set-buffer buffer)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
536 (if (= (buffer-size) 0)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
537 0
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
538 (count-lines (point-min) (point-max))))))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
539 (cond ((= lines 0)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
540 (display-message
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
541 'command
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
542 (if (eql exit-status 0)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
543 "(Shell command succeeded with no output)"
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
544 "(Shell command failed with no output)"))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
545 (kill-buffer buffer))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
546 ((= lines 1)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
547 (message "%s"
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
548 (save-excursion
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
549 (set-buffer buffer)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
550 (goto-char (point-min))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
551 (buffer-substring (point)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
552 (progn (end-of-line)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
553 (point))))))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
554 (t
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
555 (set-window-start (display-buffer buffer) 1))))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
556 ;; No output; error?
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
557 (let ((output
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
558 (if (and error-file
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
559 (< 0 (nth 7 (file-attributes error-file))))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
560 "some error output"
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
561 "no output")))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
562 (cond ((null exit-status)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
563 (message "(Shell command failed with error)"))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
564 ((equal 0 exit-status)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
565 (message "(Shell command succeeded with %s)"
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
566 output))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
567 ((stringp exit-status)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
568 (message "(Shell command killed by signal %s)"
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
569 exit-status))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
570 (t
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
571 (message "(Shell command failed with code %d and %s)"
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
572 exit-status output))))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
573 ;; Don't kill: there might be useful info in the undo-log.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
574 ;; (kill-buffer buffer)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
575 ))))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
576
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
577 (when (and error-file (file-exists-p error-file))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
578 (if (< 0 (nth 7 (file-attributes error-file)))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
579 (with-current-buffer (get-buffer-create error-buffer)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
580 (let ((pos-from-end (- (point-max) (point))))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
581 (or (bobp)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
582 (insert "\f\n"))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
583 ;; Do no formatting while reading error file,
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
584 ;; because that can run a shell command, and we
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
585 ;; don't want that to cause an infinite recursion.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
586 (format-insert-file error-file nil)
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
587 ;; Put point after the inserted errors.
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
588 (goto-char (- (point-max) pos-from-end)))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
589 (and display-error-buffer
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
590 (display-buffer (current-buffer)))))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
591 (delete-file error-file))
11da5b828d10 shell-command and shell-command-on-region API compliant with FSF 23.3.1
Mats Lidell <mats.lidell@cag.se>
parents: 5402
diff changeset
592 exit-status))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594 (defun shell-quote-argument (argument)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595 "Quote an argument for passing as argument to an inferior shell."
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
596 (if (and (eq system-type 'windows-nt)
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
597 (let ((progname (downcase (file-name-nondirectory
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
598 shell-file-name))))
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
599 (or (equal progname "command.com")
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
600 (equal progname "cmd.exe"))))
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
601 ;; the expectation is that you can take the result of
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
602 ;; shell-quote-argument and pass it to as an arg to
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
603 ;; (start-process shell-quote-argument ...) and have it end
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
604 ;; up as-is in the program's argv[] array. to do this, we
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
605 ;; need to protect against both the shell's and the program's
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
606 ;; quoting conventions (and our own conventions in
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
607 ;; mswindows-construct-process-command-line!). Putting quotes
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
608 ;; around shell metachars gets through the last two, and applying
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
609 ;; the normal VC runtime quoting works with practically all apps.
776
79940b592197 [xemacs-hg @ 2002-03-15 07:43:14 by ben]
ben
parents: 771
diff changeset
610 (declare-fboundp (mswindows-quote-one-vc-runtime-arg argument t))
611
38db05db9cb5 [xemacs-hg @ 2001-06-08 12:21:09 by ben]
ben
parents: 444
diff changeset
611 (if (equal argument "")
38db05db9cb5 [xemacs-hg @ 2001-06-08 12:21:09 by ben]
ben
parents: 444
diff changeset
612 "\"\""
38db05db9cb5 [xemacs-hg @ 2001-06-08 12:21:09 by ben]
ben
parents: 444
diff changeset
613 ;; Quote everything except POSIX filename characters.
38db05db9cb5 [xemacs-hg @ 2001-06-08 12:21:09 by ben]
ben
parents: 444
diff changeset
614 ;; This should be safe enough even for really weird shells.
38db05db9cb5 [xemacs-hg @ 2001-06-08 12:21:09 by ben]
ben
parents: 444
diff changeset
615 (let ((result "") (start 0) end)
38db05db9cb5 [xemacs-hg @ 2001-06-08 12:21:09 by ben]
ben
parents: 444
diff changeset
616 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
38db05db9cb5 [xemacs-hg @ 2001-06-08 12:21:09 by ben]
ben
parents: 444
diff changeset
617 (setq end (match-beginning 0)
38db05db9cb5 [xemacs-hg @ 2001-06-08 12:21:09 by ben]
ben
parents: 444
diff changeset
618 result (concat result (substring argument start end)
38db05db9cb5 [xemacs-hg @ 2001-06-08 12:21:09 by ben]
ben
parents: 444
diff changeset
619 "\\" (substring argument end (1+ end)))
38db05db9cb5 [xemacs-hg @ 2001-06-08 12:21:09 by ben]
ben
parents: 444
diff changeset
620 start (1+ end)))
38db05db9cb5 [xemacs-hg @ 2001-06-08 12:21:09 by ben]
ben
parents: 444
diff changeset
621 (concat result (substring argument start))))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
623 (defun shell-command-to-string (command)
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
624 "Execute shell command COMMAND and return its output as a string."
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
625 (with-output-to-string
1189
23eaac87b4b0 [xemacs-hg @ 2003-01-05 18:38:36 by daiki]
daiki
parents: 930
diff changeset
626 (with-current-buffer standard-output
23eaac87b4b0 [xemacs-hg @ 2003-01-05 18:38:36 by daiki]
daiki
parents: 930
diff changeset
627 (call-process shell-file-name nil t nil shell-command-switch command))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
628
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 428
diff changeset
629 (defalias 'exec-to-string 'shell-command-to-string)
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
630
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
631
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
632 ;; History list for environment variable names.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
633 (defvar read-envvar-name-history nil)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
634
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
635 (defun read-envvar-name (prompt &optional mustmatch)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
636 "Read environment variable name, prompting with PROMPT.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
637 Optional second arg MUSTMATCH, if non-nil, means require existing envvar name.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
638 If it is also not t, RET does not exit if it does non-null completion."
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
639 (completing-read prompt
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
640 (mapcar (function
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
641 (lambda (enventry)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
642 (list (substring enventry 0
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
643 (string-match "=" enventry)))))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
644 process-environment)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
645 nil mustmatch nil 'read-envvar-name-history))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
646
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
647 ;; History list for VALUE argument to setenv.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
648 (defvar setenv-history nil)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
649
930
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
650 (defun substitute-env-vars (string)
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
651 "Substitute environment variables referred to in STRING.
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
652 `$FOO' where FOO is an environment variable name means to substitute
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
653 the value of that variable. The variable name should be terminated
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
654 with a character not a letter, digit or underscore; otherwise, enclose
4327
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
655 the entire variable name in braces. For instance, in `ab$cd-x',
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
656 `$cd' is treated as an environment variable.
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
657
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
658 Use `$$' to insert a single dollar sign."
930
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
659 (let ((start 0))
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
660 (while (string-match
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
661 ;; XEmacs change - FSF use their rx macro to generate this regexp
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
662 "\\(?:\\$\\(\\(?:[a-zA-Z0-9_]\\)+\\)\\)\\|\\(?:\\${\\(\\(?:.\\|\n\\)*?\\)}\\)\\|\\$\\$"
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
663 string start)
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
664 (cond ((match-beginning 1)
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
665 (let ((value (getenv (match-string 1 string))))
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
666 (setq string (replace-match (or value "") t t string)
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
667 start (+ (match-beginning 0) (length value)))))
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
668 ((match-beginning 2)
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
669 (let ((value (getenv (match-string 2 string))))
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
670 (setq string (replace-match (or value "") t t string)
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
671 start (+ (match-beginning 0) (length value)))))
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
672 (t
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
673 (setq string (replace-match "$" t t string)
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
674 start (+ (match-beginning 0) 1)))))
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
675 string))
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
676
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
677 (defun setenv (variable &optional value unset substitute-env-vars)
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
678 "Set the value of the environment variable named VARIABLE to VALUE.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
679 VARIABLE should be a string. VALUE is optional; if not provided or is
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
680 `nil', the environment variable VARIABLE will be removed.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
681
930
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
682 UNSET, if non-nil, means to remove VARIABLE from the environment.
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
683 SUBSTITUTE-ENV-VARS, if non-nil, means to substitute environment
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
684 variables in VALUE using `substitute-env-vars'.
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
685
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
686 Interactively, a prefix argument means to unset the variable.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
687 Interactively, the current value (if any) of the variable
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
688 appears at the front of the history list when you type in the new value.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
689
4327
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
690 This function works by modifying `process-environment'.
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
691
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
692 As a special case, setting variable `TZ' calls `set-time-zone-rule' as
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
693 a side-effect."
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
694 (interactive
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
695 (if current-prefix-arg
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
696 (list (read-envvar-name "Clear environment variable: " 'exact) nil t)
4327
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
697 (let* ((var (read-envvar-name "Set environment variable: " nil))
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
698 (value (getenv var)))
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
699 (when value
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
700 (push value setenv-history))
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
701 ;; Here finally we specify the args to call setenv with.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
702 (list var (read-from-minibuffer (format "Set %s to value: " var)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
703 nil nil nil 'setenv-history
4327
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
704 ;; XEmacs change; don't specify a
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
705 ;; default. (Nor an abbrev table.)
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
706 )))))
930
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
707 (if unset
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
708 (setq value nil)
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
709 (if substitute-env-vars
eaedf30d9d76 [xemacs-hg @ 2002-07-23 08:34:59 by youngs]
youngs
parents: 921
diff changeset
710 (setq value (substitute-env-vars value))))
4327
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
711
4611
9c97a5a8c241 Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents: 4610
diff changeset
712 ;; GNU fuck around with coding systems here. We do it at a much lower
9c97a5a8c241 Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents: 4610
diff changeset
713 ;; level; an equivalent of the following code of Handa's would be
9c97a5a8c241 Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents: 4610
diff changeset
714 ;; worthwhile here, though:
9c97a5a8c241 Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents: 4610
diff changeset
715
9c97a5a8c241 Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents: 4610
diff changeset
716 ; (let ((codings (find-coding-systems-string (concat variable value))))
9c97a5a8c241 Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents: 4610
diff changeset
717 ; (unless (or (eq 'undecided (car codings))
9c97a5a8c241 Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents: 4610
diff changeset
718 ; (memq (coding-system-base locale-coding-system) codings))
9c97a5a8c241 Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents: 4610
diff changeset
719 ; (error "Can't encode `%s=%s' with `locale-coding-system'"
9c97a5a8c241 Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents: 4610
diff changeset
720 ; variable (or value "")))))
9c97a5a8c241 Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents: 4610
diff changeset
721
9c97a5a8c241 Backed out changeset 38e8af61f38d
Aidan Kehoe <kehoea@parhasard.net>
parents: 4610
diff changeset
722 ;; But then right now our find-coding-systems analogue is in packages.
4327
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
723
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
724 (if (string-match "=" variable)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
725 (error "Environment variable name `%s' contains `='" variable)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
726 (let ((pattern (concat "\\`" (regexp-quote (concat variable "="))))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
727 (case-fold-search nil)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
728 (scan process-environment)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
729 found)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
730 (if (string-equal "TZ" variable)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
731 (set-time-zone-rule value))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
732 (while scan
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
733 (cond ((string-match pattern (car scan))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
734 (setq found t)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
735 (if (eq nil value)
5652
cc6f0266bc36 Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents: 5555
diff changeset
736 (setq process-environment
cc6f0266bc36 Avoid #'delq in core Lisp, for the sake of style, a very slightly smaller binary
Aidan Kehoe <kehoea@parhasard.net>
parents: 5555
diff changeset
737 (delete* (car scan) process-environment))
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
738 (setcar scan (concat variable "=" value)))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
739 (setq scan nil)))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
740 (setq scan (cdr scan)))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
741 (or found
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
742 (if value
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
743 (setq process-environment
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
744 (cons (concat variable "=" value)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
745 process-environment)))))))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
746
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
747 ;; already in C. Can't move it to Lisp too easily because it's needed
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
748 ;; extremely early in the Lisp loadup sequence.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
749
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
750 ; (defun getenv (variable)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
751 ; "Get the value of environment variable VARIABLE.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
752 ; VARIABLE should be a string. Value is nil if VARIABLE is undefined in
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
753 ; the environment. Otherwise, value is a string.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
754 ;
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
755 ; This function consults the variable `process-environment'
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
756 ; for its value."
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
757 ; (interactive (list (read-envvar-name "Get environment variable: " t)))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
758 ; (let ((value (getenv-internal variable)))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
759 ; (when (interactive-p)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
760 ; (message "%s" (if value value "Not set")))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
761 ; value))
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
762
4327
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
763
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
764 ;; GNU have an #'environment function here, as of 2007-12-14. If someone
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
765 ;; actually uses it in the wild, this would suffice as an implementation:
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
766
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
767 ; (defun environment (&optional frame)
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
768 ; "Return a list of environment variables with their values.
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
769 ; Each entry in the list is a string of the form NAME=VALUE.
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
770 ;
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
771 ; Optional argument FRAME is ignored, for GNU compatibility.
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
772 ;
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
773 ; Non-ASCII characters look like Mojibake (that is, they are unreadable.)"
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
774 ; (loop
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
775 ; for entry in process-environment
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
776 ; collect (encode-coding-string entry 'native)))
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
777 ;
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
778
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
779 ;; but we shouldn't encourage that sort of ugliness and needless backwards
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
780 ;; incompatibility.
466ad8ad5f13 Fix a #'setenv bug, merge other changes from GNU's env.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 3491
diff changeset
781
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
782 (provide 'env) ;; Yuck. Formerly the above were in env.el, which did this
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 611
diff changeset
783 ;; provide.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
784
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
785 ;;; process.el ends here