[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Interactive shell commands

From: Sebastian Tennant
Subject: Interactive shell commands
Date: Tue, 20 Jan 2009 21:50:44 +0000
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/22.2 (gnu/linux)

Hi all,

These two interactive functions (Emacs commands) spawn interactive
shells, useful for invoking any shell alias commands you may have
defined in the same way direct shell commands are invoked using M-!

They've only been tested with bash.  I've no idea whether other shells
accept a '-i' switch to force interactive behaviour.

Asynchronous commands can be executed either by calling
asynchronous-interactive-shell-command directly or by calling
interactive-shell-command and entering a command that ends with a
whitespace-separated ampersand.

If an asynchronous command invocation is prefixed with a universal
argument (C-u) then the buffer *Async Shell Command* is _not_ displayed
as it otherwise would be.

As they stand, it is not possible to insert output into a buffer of
choice or divert stderr elsewhere, although this would be fairly easy to
implement if you know how.

It's always easy when you know how!!!

Hope you find them useful.  They work for me.



;;; asynchronous process sentinel
(defun shell-command-sentinel (process signal)
  (if (memq (process-status process) '(exit signal))
      (message "%s: %s."
               (car (cdr (cdr (cdr (process-command process)))))
               (substring signal 0 -1))))

(defun interactive-shell-command (command)
  (interactive "sCommand: ")
  (if (string-match  "[ \t]*&[ \t]*\\'" command)
      ;; asynchronous command
        (substring command 0 (match-beginning 0))) ;strip ampersand
      (set-buffer (get-buffer-create "*Shell Command Output*"))
      (let ((exit-code (call-process shell-file-name nil t t "-i" "-c" 
        (if (> (buffer-size) 0)
            (display-buffer (current-buffer))
          (progn (kill-buffer nil)
                 (if (eq exit-code 0)
                     (message "(Shell command succeeded with no output)")
                   (message "(Shell command failed with code %d and no output)"

(defun asynchronous-interactive-shell-command (command)
  (interactive "sCommand: ")
  (set-buffer (get-buffer-create "*Async Shell Command*"))
  (if (get-buffer-process (current-buffer))
      (progn (message "(An asynchronous process is already in progress)")
             (unless current-prefix-arg (display-buffer (current-buffer))))
    (progn (erase-buffer)
              (current-buffer) shell-file-name nil "-i" "-c" command))
             (setq mode-line-process '(":%s"))
             (unless current-prefix-arg (display-buffer (current-buffer))))))


reply via email to

[Prev in Thread] Current Thread [Next in Thread]