How do I run
(async-)shell-command (but no other
shell-mode buffers) in
How do I run
Do can define your another wrapper function that calls
async-shell-command which enables the
view-mode after calling that function.
(defun my/async-shell-command (command &optional output-buffer error-buffer) (interactive (list (read-shell-command "Async shell command: " nil nil (let ((filename (cond (buffer-file-name) ((eq major-mode 'dired-mode) (dired-get-filename nil t))))) (and filename (file-relative-name filename)))) current-prefix-arg shell-command-default-error-buffer)) ;; call the original function (async-shell-command command output-buffer error-buffer) ;; switch to the shell command output buffer (switch-to-buffer "*Async Shell Command*") ;; enable `view-mode' (view-mode))
and bind that to the default
Note that the
(interactive (list ..)) portion of the wrapper funciton is copied directly from the source code for
async-shell-command. It's useful to retain the input arguments of the original function when writing a wrapper around it. The same applies if you were to use
advice-add (emacs 24.4+) instead.
Similar thing can be done for
Seeing as there are no shell command hooks, an alternative is to use advice. All asynchronous processes need to specify a sentinel function, which in the case of shell commands is
(define-advice shell-command-sentinel (:after (process _msg) my-view-output) "Enable `view-mode' in `*Async Shell Command*' buffers." (let ((buffer (process-buffer process))) (and (memq (process-status process) '(exit signal)) (buffer-live-p buffer) (with-current-buffer buffer (view-mode)))))
Synchronous shell commands, on the other hand, all go through the command
(define-advice shell-command-on-region (:after (&rest _) my-view-output) "Enable `view-mode' in `*Shell Command Output*' buffer." (let ((buffer (get-buffer "*Shell Command Output*"))) (when (buffer-live-p buffer) (with-current-buffer buffer (view-mode)))))
The latter advice is actually a bit more brittle than the former because it hard-codes the buffer name, but it should work for most cases. You should tweak it to suit your needs and use of shell commands.