tags:

views:

83

answers:

2

Here's what I would like to do: When C-c C-l is pressed, a new terminal window is launched if there is no terminal window already, then, in that terminal, gcc is invoked with some flags and the current buffer's file. How would I do that?

+4  A: 

Try the built in:

M-x compile gcc ...

compile isn't bound to any keys by default, but you could do something like:

(add-hook 'c-mode-common-hook 
          (lambda () (define-key c-mode-base-map (kbd "C-c C-l") 'compile))))

There are a bunch of packages people have written around compiling on the Emacs Wiki. Check out SmartCompile, CompileCommand, and the category Programmer Utils.

The benefit of using M-x compile ... over just running it in a "terminal" is that you get C-x ` (aka next-error) which will jump you to the file and line that caused the error command. And there's the command M-x recompile which does what you'd expect. And, of course, like all Emacs commands, the compile command keeps a history of the compile calls and you can go through the history with M-p and M-n.

Trey Jackson
You should note that after running `M-x compile` once you can use `M-x recompile` to run the same command as last time.
mathepic
A: 

I do exactly this to run my perl unit tests in a nearby eshell: http://github.com/jrockway/elisp/blob/master/_local/cperl-extras.el#L15

The general shape of the code is:

(defun run-in-eshell (cmd use-hidden-eshell-p)
  (with-current-buffer (eproject-eshell-cd-here use-hidden-eshell-p)
    (eshell-preinput-scroll-to-bottom)
    (goto-char (point-max))
    (insert cmd)
    (eshell-send-input nil t)
    (goto-char (point-max))
    (ignore-errors
      (set-window-point (get-buffer-window) (point-max)))))

eproject-eshell-cd-here is what actually finds the eshell buffer. If you don't want to install eproject, then you can just loop through the buffers and find it yourself.

jrockway