Saturday, November 7, 2009

HOWTO: setup emacs to run scripts within UI

Overview
Lately, I have gone back to my old friend... emacs. Some would agree that a better and more flexible editor has never been created. I used emacs for many years for C/C++, perl, HTML, and shell programming. Over time, projects and jobs drove me to full IDEs like Netbeans and Visual Studio.

I recently purchased a DELL Mini 10v netbook running (of course) Ubuntu. With the tiny (but beautiful and clear) screen, big IDEs are just not practical. Even with all panels minimized there is still only a tiny amount of space.

For my last trip, I needed to develop some code to hack a pile data I was emailed right before I got on the plane... stat! Thank ___ for having a plane friendly computer like a netbook.

I have become dependent on all the work heavy IDEs automatically do. Do I really know who to code or does the IDE guide me to a completed program? Plus, data hacking is better for scripting languages like python or perl. On taxi I thought it over and made my call. Welcome back to my tool belt emacs.

Once we hit cruising altitude, I booted up Ubuntu 9.04, fired up emacs and started coding. There was one problem. I have been so used to simply pressing F6 to run my program. I wanted to do the same thing in emacs. Why be limited?

The following tells you howto setup emacs, for this type of behavior, using just a small amount of lisp code. The only real dependency is having the languages you are using in the path. If python or perl doesn't run from a command prompt, this will not work very well.

Requirements
If you are using linux, OSX, or Solaris you will edit your ~/.emacs file. If you are running Windows, you will be editing your %HOME%/_emacs

For this howto, emacs-22 is being used under Ubuntu Linux 9.10. I doubt the OS or version of emacs will make a difference.

Launch Interpreter using F5
Simply open your .emacs or _emacs file and add the following
;; ============================================================================
;; Runs Scripting Languages Program using compile key bindings
;;
;; if you have another lanugage, simply create a ____-run defun and add an if
;; statement to script-run()
;; ============================================================================

(defun python-run ()
"Use compile to run python programs"
(interactive)
(message "Running python for buffer %s." (buffer-name))
(compile (concat "python " (buffer-name)))
)
(defun perl-run ()
"Use compile to run perl programs"
(interactive)
(message "Running perl for buffer %s." (buffer-name))
(compile (concat "perl -w " (buffer-name)))
)
(defun script-run ()
"Use to run a script"
(interactive)
(if (equal (file-name-extension(buffer-name)) "py")
(python-run))
(if (equal (file-name-extension(buffer-name)) "pl")
(perl-run))
)
(global-set-key [f5] 'script-run)

If the file is python (py) then python will be run in a separate buffer. If the file is perl (pl) then perl will be run in a separate buffer. To trigger this, all that is needed is to press F5 while in a buffer that is either a python or perl file. That is it.

Final Thoughts
This approach can easily be expanded for other languages like ruby and such. I hope this helps simplify programming using emacs. Find my full .emacs file here.

Personally, I am glad I dusted off emacs. Humm... what about VI.

1 comment:

Anonymous said...

wow thanks so much!! this was exactly what I was looking for and spend an hour looking before I stumbled on this post!!