comparison lisp/simple.el @ 24155:62548105541c

(shell-command-on-region): Return command's exit status.
author Dave Love <fx@gnu.org>
date Sat, 23 Jan 1999 21:50:18 +0000
parents ea58bb66d0e3
children b09b949b5433
comparison
equal deleted inserted replaced
24154:91c00b394901 24155:62548105541c
1 ;;; simple.el --- basic editing commands for Emacs 1 ;;; simple.el --- basic editing commands for Emacs
2 2
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 1998 3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 98, 1999
4 ;; Free Software Foundation, Inc. 4 ;; Free Software Foundation, Inc.
5 5
6 ;; This file is part of GNU Emacs. 6 ;; This file is part of GNU Emacs.
7 7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify 8 ;; GNU Emacs is free software; you can redistribute it and/or modify
1182 (defun shell-command-on-region (start end command 1182 (defun shell-command-on-region (start end command
1183 &optional output-buffer replace 1183 &optional output-buffer replace
1184 error-buffer) 1184 error-buffer)
1185 "Execute string COMMAND in inferior shell with region as input. 1185 "Execute string COMMAND in inferior shell with region as input.
1186 Normally display output (if any) in temp buffer `*Shell Command Output*'; 1186 Normally display output (if any) in temp buffer `*Shell Command Output*';
1187 Prefix arg means replace the region with it. 1187 Prefix arg means replace the region with it. Return the exit code of
1188 COMMAND.
1188 1189
1189 To specify a coding system for converting non-ASCII characters 1190 To specify a coding system for converting non-ASCII characters
1190 in the input and output to the shell command, use \\[universal-coding-system-argument] 1191 in the input and output to the shell command, use \\[universal-coding-system-argument]
1191 before this command. By default, the input (from the current buffer) 1192 before this command. By default, the input (from the current buffer)
1192 is encoded in the same coding system that will be used to save the file, 1193 is encoded in the same coding system that will be used to save the file,
1237 shell-command-on-region-default-error-buffer))) 1238 shell-command-on-region-default-error-buffer)))
1238 (let ((error-file 1239 (let ((error-file
1239 (if error-buffer 1240 (if error-buffer
1240 (concat (file-name-directory temp-file-name-pattern) 1241 (concat (file-name-directory temp-file-name-pattern)
1241 (make-temp-name "scor")) 1242 (make-temp-name "scor"))
1242 nil))) 1243 nil))
1244 exit-status)
1243 (if (or replace 1245 (if (or replace
1244 (and output-buffer 1246 (and output-buffer
1245 (not (or (bufferp output-buffer) (stringp output-buffer)))) 1247 (not (or (bufferp output-buffer) (stringp output-buffer))))
1246 (equal (buffer-name (current-buffer)) "*Shell Command Output*")) 1248 (equal (buffer-name (current-buffer)) "*Shell Command Output*"))
1247 ;; Replace specified region with output from command. 1249 ;; Replace specified region with output from command.
1248 (let ((swap (and replace (< start end)))) 1250 (let ((swap (and replace (< start end))))
1249 ;; Don't muck with mark unless REPLACE says we should. 1251 ;; Don't muck with mark unless REPLACE says we should.
1250 (goto-char start) 1252 (goto-char start)
1251 (and replace (push-mark)) 1253 (and replace (push-mark))
1252 (call-process-region start end shell-file-name t 1254 (setq exit-status
1253 (if error-file 1255 (call-process-region start end shell-file-name t
1254 (list t error-file) 1256 (if error-file
1255 t) 1257 (list t error-file)
1256 nil shell-command-switch command) 1258 t)
1259 nil shell-command-switch command))
1257 (let ((shell-buffer (get-buffer "*Shell Command Output*"))) 1260 (let ((shell-buffer (get-buffer "*Shell Command Output*")))
1258 (and shell-buffer (not (eq shell-buffer (current-buffer))) 1261 (and shell-buffer (not (eq shell-buffer (current-buffer)))
1259 (kill-buffer shell-buffer))) 1262 (kill-buffer shell-buffer)))
1260 ;; Don't muck with mark unless REPLACE says we should. 1263 ;; Don't muck with mark unless REPLACE says we should.
1261 (and replace swap (exchange-point-and-mark))) 1264 (and replace swap (exchange-point-and-mark)))
1262 ;; No prefix argument: put the output in a temp buffer, 1265 ;; No prefix argument: put the output in a temp buffer,
1263 ;; replacing its entire contents. 1266 ;; replacing its entire contents.
1264 (let ((buffer (get-buffer-create 1267 (let ((buffer (get-buffer-create
1265 (or output-buffer "*Shell Command Output*"))) 1268 (or output-buffer "*Shell Command Output*")))
1266 (success nil) 1269 (success nil))
1267 (exit-status nil))
1268 (unwind-protect 1270 (unwind-protect
1269 (if (eq buffer (current-buffer)) 1271 (if (eq buffer (current-buffer))
1270 ;; If the input is the same buffer as the output, 1272 ;; If the input is the same buffer as the output,
1271 ;; delete everything but the specified region, 1273 ;; delete everything but the specified region,
1272 ;; then replace that region with the output. 1274 ;; then replace that region with the output.
1277 (call-process-region (point-min) (point-max) 1279 (call-process-region (point-min) (point-max)
1278 shell-file-name t 1280 shell-file-name t
1279 (if error-file 1281 (if error-file
1280 (list t error-file) 1282 (list t error-file)
1281 t) 1283 t)
1282 nil shell-command-switch command)) 1284 nil shell-command-switch
1283 (setq success t)) 1285 command)))
1284 ;; Clear the output buffer, then run the command with output there. 1286 ;; Clear the output buffer, then run the command with
1287 ;; output there.
1285 (save-excursion 1288 (save-excursion
1286 (set-buffer buffer) 1289 (set-buffer buffer)
1287 (setq buffer-read-only nil) 1290 (setq buffer-read-only nil)
1288 (erase-buffer)) 1291 (erase-buffer))
1289 (setq exit-status 1292 (setq exit-status
1290 (call-process-region start end shell-file-name nil 1293 (call-process-region start end shell-file-name nil
1291 (if error-file 1294 (if error-file
1292 (list buffer error-file) 1295 (list buffer error-file)
1293 buffer) 1296 buffer)
1294 nil shell-command-switch command)) 1297 nil shell-command-switch command)))
1295 (setq success t)) 1298 (setq success (zerop exit-status))
1296 ;; Report the amount of output. 1299 ;; Report the amount of output.
1297 (let ((lines (save-excursion 1300 (let ((lines (save-excursion
1298 (set-buffer buffer) 1301 (set-buffer buffer)
1299 (if (= (buffer-size) 0) 1302 (if (= (buffer-size) 0)
1300 0 1303 0
1321 (if (and error-file (file-exists-p error-file)) 1324 (if (and error-file (file-exists-p error-file))
1322 (save-excursion 1325 (save-excursion
1323 (set-buffer (get-buffer-create error-buffer)) 1326 (set-buffer (get-buffer-create error-buffer))
1324 ;; Do no formatting while reading error file, for fear of looping. 1327 ;; Do no formatting while reading error file, for fear of looping.
1325 (format-insert-file error-file nil) 1328 (format-insert-file error-file nil)
1326 (delete-file error-file))))) 1329 (delete-file error-file)))
1330 exit-status))
1327 1331
1328 (defun shell-command-to-string (command) 1332 (defun shell-command-to-string (command)
1329 "Execute shell command COMMAND and return its output as a string." 1333 "Execute shell command COMMAND and return its output as a string."
1330 (with-output-to-string 1334 (with-output-to-string
1331 (with-current-buffer 1335 (with-current-buffer