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