MattyW
MattyW

Reputation: 1529

Evaluating a buffer based on filetype in emacs

Currently in Vim I have the following setup:

au Bufenter *.clj map <F5> :!clojure1.2 %<CR> 
au Bufenter *.py map <F5> :!python %<CR> 

Which, in essence, means when I open a new buffer with a .clj extension it binds f5 to eval the current buffer using the command line command "clojure1.2 filename".The following line is a binding for python files.

My question is - how can I achieve this same sort of thing in Emacs? I've been looking for a few weeks but can't find anything simple. I appreciate there are clojure and python modes for me to try. But i'd be interested to see if I can achieve the above in a few lines of elisp.

Upvotes: 1

Views: 100

Answers (1)

Trey Jackson
Trey Jackson

Reputation: 74450

Something like this should do the trick:

(defun run-it ()
  "Run the appropriate executable with the file of the current buffer as input."
  (interactive)
  (let ((command (cdr (assq major-mode '((clojure-mode . "clojure1.2")
                                         (python-mode . "python"))))))
    (unless command
      (error "No command found for major mode: %s" major-mode))
    (shell-command (format "%s %s" command (buffer-file-name)))))
(define-key python-mode-map (kbd "<f5>") 'run-it)
(define-key clojure-mode-map (kbd "<f5>") 'run-it)

Upvotes: 4

Related Questions