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