[gnu.emacs.bug] Small change to shell command

king@KESTREL.EDU (Dick King) (09/16/89)

Lots of shell commands reply with one line: it's annoying to have your
window structure rearranged to show that line in the *Shell Command Output* 
buffer.

Instead, I message single lines in the minibuffer.

Code:

(defun shell-command-on-region (start end command &optional flag interactive)
  "Execute string COMMAND in inferior shell with region as input.
Normally display output (if multiline) in temp buffer;
  single line output is shown in minibuffer.
Prefix arg means replace the region with it.
Noninteractive args are START, END, COMMAND, FLAG.
Noninteractively FLAG means insert output in place of text from START to END,
and put point at the end, but don't alter the mark."
  (interactive "r\nsShell command on region: \nP\np")
  (if flag
      ;; Replace specified region with output from command.
      (let ((swap (and interactive (< (point) (mark)))))
	;; Don't muck with mark
	;; unless called interactively.
	(and interactive (push-mark))
	(call-process-region start end shell-file-name t t nil
			     "-c" command)
	(and interactive swap (exchange-point-and-mark)))
    (let ((buffer (get-buffer-create "*Shell Command Output*")))
      (save-excursion
	(set-buffer buffer)
	(erase-buffer))
      (if (eq buffer (current-buffer))
	  (setq start 1 end 1))
      (call-process-region start end shell-file-name
			   nil buffer nil
			   "-c" command)
      (if (save-excursion
	    (set-buffer buffer)
	    (> (count-lines (dot-min) (dot-max)) 1))
	  (set-window-start (display-buffer buffer) 1)
	(message (save-excursion
		   (set-buffer buffer)
		   (if (zerop (buffer-size))
		       "(Shell command completed with no output)"
		     (concat "reply: "
			     (buffer-substring
			      1
			      (if (= (char-after (1- (dot-max))) ?\n)
				  (1- (dot-max))
				(dot-max)))))))))))



-dk