Emacs Lisp: Run Current File
This page shows a emacs lisp command that execute the script you are writing of current buffer.
You are coding ruby. You want to be able to press a button, and have the current file executed or compiled. The file may be a Perl, Python, Ruby, PHP, Java, Clojure, golang, TeX/LaTeX, Bash, and more.
This way is even better than using a interactive command line (aka REPL), because you can write and edit normally with full emacs power. And you can run it by just pressing a key, such as F8.
The code is very easy to understand.
(info "(elisp) Association Lists")
(info "(elisp) Buffer File Name")
(info "(elisp) File Name Components")
Now, we can define a keyboard shortcut for this:
(global-set-key (kbd "<f8>") 'xah-run-current-file)
〔➤ Emacs: How to Define Keys〕
So now, doesn't matter we are writing in Python, ruby, golang, …, we can just press a button and have the file executed or compiled.
Determine Language by Major Mode
If you don't have file extension, you might use the current major mode to determine what language it is.
The major mode's name is stored in the variable major-mode.
You also need to redefine your associative list to store major mode names corresponding to program paths.
(thanks to Matic Bojan for suggesting major mode for determining language.)
This package, emacs-quickrun, supports more languages and features. https://github.com/syohex/emacs-quickrun/