tags:

views:

209

answers:

4

I'd like to be able to run a shell command on the current file that I'm editing and have the output be shown in the Shell Command Output window. I've been able to define the function which is shown below.

(defun cpp-check ()
  "Run cpp-check on current file the buffer is visiting."
  (shell-command
   (concat "/home/sburke/downloads/cppcheck-1.31/cppcheck "
       (buffer-file-name))))

The only problem is that the output window isn't brought to the foreground in any way. What I'd like to happen is for the window to be split and the output window shown there. Also, am I on the right track here defining the function to be put in my .emacs file or is there a better way?

Any help would be appreciated. Thanks.

+1  A: 

splitting the window is (split-window-vertically) It has an optional arg of the size of the (top if positive, bottom if negative) part of the window.

Then, and you need to do is bring the shell results buffer to the front with switch-to-buffer or switch-to-buffer-other-window.

Remember that when you spit the window (frame) in emacs, you end up with two "windows" because of a naming confusing back in the day that it's too late to deal with now...

Brian Postow
+2  A: 

Take a look at the documentation for 'shell-command, this worked well for me:

(defun cpp-check ()
  "Run cpp-check on current file the buffer is visiting."
  (shell-command
   (concat "/home/sburke/downloads/cppcheck-1.31/cppcheck "
       (buffer-file-name))
   "cpp-check"))

It creates a new buffer named "cpp-check" and puts the results there. The current frame is split in to, and the "cpp-check" buffer is put in the other window.

Trey Jackson
+2  A: 

See the function `pop-to-buffer'. I think.

You should be able to give it a buffer name to pop to -- just give in the Shell Command Output buffer.

Denis Bueno
A: 

This is what I came up with. Thanks for the responses. I defined a function that will go ahead and run cpp-check. I wanted it bound to a key in c-mode so I add it as a hook. I ran into the difference between normal functions and ones that can be bound to keymaps so I had to make the function interactive. This article helped explain that. So now when the shortcut is pressed the results come up in another window, but the cursor remains in the original buffer, which is what I want. The only problem is that the output is shown in the minibuffer as well which isn't quite what I want. Any thoughts on fixing that little detail?

(defun cpp-check ()
  (interactive)
  "Run cpp-check on current file the buffer is visiting."
  (shell-command
   (concat "/home/sburke/downloads/cppcheck-1.31/cppcheck "
       (buffer-file-name)))
  (display-buffer "*Shell Command Output*"))

(add-hook 'c-mode-common-hook
           (lambda ()
         (define-key c-mode-base-map
               "\C-x\p" 'cpp-check)))
Stephen Burke