Archive for June, 2007

Emacs Ruby Foo

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

In my last post I described how I configured emacs to have a Textmate style code browser pane. Since then I have been doing some Rails hacking and have used it a lot and found it works well. In this post I am going to describe some Ruby specific customisations.

Let there be Syntax Highlighting

For most languages, Emacs will syntax highlight your source code right out of the box. It does this by switching into a language specific mode when you open files with certain extensions. Ruby Mode is not present any of my Emacs installs out of the box, but adding support is trivial.

ruby-mode.el

In the Ruby source distribution, under the misc directory, you will find several files. The one called ruby-mode.el is the one that adds Ruby syntax highlighting support to Emacs.

Download it, place it somewhere in your Emacs load path. On windows I created a directory called C:\emacs-22.1\includes and copied ruby-mode.el into it.

(Edit – While you’re there, download inf-ruby.el and put it in the same directory – its needed later when setting up Rails mode)

First ensure the ‘includes directory’ is in the emacs load path by defining it in your _emacs file (at or near the top as it needs to be declared before you use anything in it):


; directory to put various el files into
(add-to-list 'load-path "C:/emacs-22.1/includes")

Now add the following lines to your _emacs:


; loads ruby mode when a .rb file is opened.
(autoload 'ruby-mode "ruby-mode" "Major mode for editing ruby scripts." t)
(setq auto-mode-alist  (cons '(".rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist  (cons '(".rhtml$" . html-mode) auto-mode-alist))

Now when you open a file with an rb extension, ruby-mode will automatically activate. Also, if you open any rhtml files, html-mode will activate which is useful in Rails apps.

Adding Electric

Before saying anything more about ruby-mode, we may as well install ruby-electric mode. I am still in two minds if I like this feature or not, but I am living with it for now. Basically electric-mode defines ‘electric keys’. For example, if you type a quote, the closing quote will automatically be inserted. Same for {, [, and (. Also typing ‘def ‘ will result in the corresponding end being inserted too. Its easier if you play with it a little to see what I mean!

To install electric-mode, download the ruby-electric.el file from the Ruby misc directory into your include directory, and add the following to your _emacs:


(add-hook 'ruby-mode-hook
          (lambda()
            (add-hook 'local-write-file-hooks
                      '(lambda()
                         (save-excursion
                           (untabify (point-min) (point-max))
                           (delete-trailing-whitespace)
                           )))
            (set (make-local-variable 'indent-tabs-mode) 'nil)
            (set (make-local-variable 'tab-width) 2)
            (imenu-add-to-menubar "IMENU")
            (define-key ruby-mode-map "C-m" 'newline-and-indent) ;Not sure if this line is 100% right but it works!
            (require 'ruby-electric)
            (ruby-electric-mode t)
            ))

Now, we have Ruby Syntax highlighting, electric keys, and auto-indenting, but we can do more …

Messed up you indentation?

If you let you code get into a bit of a twist, you can fix the indenting by press the TAB key while on the line – give it a try, moving over each line and pressing TAB on each to correct the indents!

Comment a block

If you want to comment out a large section of code, you can select it and the type ALT-x ruby-encomment-region and it will place a ‘#’ symbol at the beginning of each line. ALT-x ruby-decomment-region does the opposite.

Compile and Run Code in Emacs

There are many ways to run code in Emacs. The easiest out-of-the-box way, is to type ALT-x compile. Emacs will prompt for a compile command, which by default is ‘make -k’. Replace this with:

ruby -w my_file_name.rb

The emacs window will split in two and the results of running the script will be in the new window.

If you are writing a text document that includes code snippets, and you want to ensure they are correct, you can select the region and type ALT-| to run it. Again, you need to enter the ruby command to run the code.

Smarter Compile?

All that typing ruby -w filename will start to annoy you after a while – the solution is mode-compile which has some brains built in. It can tell when you are editing a Ruby file (or many other types of file) and run it with the correct compiler/interpreter automatically. Download mode-compile.el and put it in your includes directory. As usual, add the following to your _emacs:


; Install mode-compile to give friendlier compiling support!
(autoload 'mode-compile "mode-compile"
   "Command to compile current buffer file based on the major mode" t)
(global-set-key "C-cc" 'mode-compile)
(autoload 'mode-compile-kill "mode-compile"
 "Command to kill a compilation launched by `mode-compile'" t)
(global-set-key "C-ck" 'mode-compile-kill)

Now you can compile/run code by typing CTRL-c c (and if your code enters a nasty infinite loop, you can kill it with CTRL-c k). The output will again appear in a new split window. If there are any compile errors in the output, you can move the cursor over them and hit return and emacs will jump to the offending line in your source file – pretty neat eh?

That is enough for this post – next time I will add Rails snippets and possibly irb support into our Emacs install.

Advertisements

June 21, 2007 at 6:16 pm 11 comments

The Emacs Code Browser

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

This is the third post in a series where I have been talking about why I decided to learn emacs and getting started with it on Windows or OS X. In this post, I will explain some of the customisations I made to fulfil my Rails development wishlist.

File Browsing

Textmate boasts an impressive feature, which is a file browsing pane, giving you easy access to all the files in your project. Luckily Emacs has something similar, called the Emacs Code Browser (ECB), but you have to figure out how to install it, which is not difficult, but it takes some time.

Downloads

Unfortunately, ECB relies on the presence of three other Emacs plugins so the first thing is to gather up all the files you need.

  • Semantic is used to help build text parsing applications in Emacs. Don’t worry about it beyond installing. I grabbed semantic-1.4.4 from here.
  • Eieio is also required. Again, I am not sure what its for beyond being required. I got eieio-0.17 here.
  • Speedbar is a code browser, that is also used by ECB. I downloaded speedbar-0.14beta4 here.
  • Finally, get ECB itself

Installing

Now we have the 4 downloads, we need to put them somewhere. On Windows, I created a directory called plugins inside my emacs directory, giving me C:\emacs-22.1\plugins

On the Mac, I created a directory called .emacs_includes/plugins in my home directory.

I am pretty sure Emacs gurus would tell me these files should go somewhere else, but this works for me and the location is not really important. Extract each of the 4 downloads into the plugins directory.

Installing Emacs plugins generally involves putting some files in a known location, and telling Emacs about them using the .emacs file. Open your .emacs file (or _emacs see the last post for more information) and add the following (this is a Windows example, edit the paths for OS X):

 
;Allows syntax highlighting to work, among other things
(global-font-lock-mode 1)
;These lines are required for ECB
(add-to-list 'load-path "C:/emacs-22.1/plugins/eieio-0.17")
(add-to-list 'load-path "C:/emacs-22.1/plugins/speedbar-0.14beta4")
(add-to-list 'load-path "C:/emacs-22.1/plugins/semantic-1.4.4")
(setq semantic-load-turn-everything-on t)
(require 'semantic-load)
; This installs ecb - it is activated with M-x ecb-activate
(add-to-list 'load-path "C:/emacs-22.1/plugins/ecb-2.32")
(require 'ecb-autoloads)

Loading ECB

Restart Emacs and start ECB by typing ALT-x ecb-activate

The Emacs window will change, adding a new section with 4 windows on the left side. In this default mode, the top window shows directories, next files, then history and finally methods in the current file.

To close ECB enter ALT-x ecb-deactivate

I changed this default layout to show only a combined directory and file listing and opened file history by adding the following lines to my .emacs:


(setq ecb-layout-name "left14")
(setq ecb-layout-window-sizes (quote (("left14" (0.2564102564102564 . 0.6949152542372882) (0.2564102564102564 . 0.23728813559322035)))))

The final thing you need to do, is add a list of directories containing the files you wish to browse, for example:


(setq ecb-source-path (quote ("d:/myRailsProject" "d:/useful scripts")))

There is a lot to configure in ECB if you wish. All the details are available in the packaged manual, which you can view by typing ALT-x ecb-show-help.

Some commands I have learned so far include:

  • Jump to the directory window CTRL-. gd
  • Jump to the history window CTRL-. gh
  • Jump to the last window you were in CTRL-. gl
  • Jump to the first editor window CTRL-. g1

The directory browser can be controlled without using the mouse too – just use the arrow keys and enter – give it a go!

As you can see, setting up ECB is not difficult, and its well worth it in my opinion. In the next article, I will add some Ruby customisations to our Emacs installation.

June 13, 2007 at 4:33 pm 15 comments

Getting started

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

In the last post in this series, I talked about why I decided to learn Emacs. In this post, I am going to explain how to get Emacs up and running and go over some essential stuff.

Installing

If you are using a Mac, I recommend getting Aquamacs Emacs. Its basically Emacs, but with a nice Mac look and feel. Installing is simple, just download the universal binary, open and drag the Aquamac Emacs icon into the applications folder.

On windows its almost as simple. Download the zip file (I grabbed emacs-22.1 from here), and extract it into any folder on your machine, I choose C:\, which placed Emacs in C:\emacs-22.1

.emacs?

Emacs is highly customisable, and all the settings relevant to your setup are stored in a file called .emacs or _emacs. Emacs searches for this file in your home directory, which is fine on OS X and Linux, but what about windows where you don’t really have a home directory?

The trick is to create an environment variable called HOME that contains the location of a directory you wish to use. A sensible place to store your _emacs is in the Application Data folder, normally located at C:\documents and settings\*username*\Application Data

To create the environment variable:

  1. Right Click on My Computer and select properties
  2. Click on the Advanced tab
  3. Click the environment variable button at the bottom
  4. Click the new button under the User variables for username pane
  5. Enter HOME as the variable name, and the location of the directory that will contain your _emacs file as the value

Playtime

Now its time to fire up emacs. On the Mac, run the Aquamac Emacs application you have just installed, on Windows double click on C:\emacs-22.1\bin\emacs.exe

The first thing you will notice is that Emacs behaves just like most other text editors – you can type stuff, open, close and save files using the menus etc. However, to start on the road to becoming a power user, you have to start learning the keyboard commands of which there are many!

The best way to learn is to work your way through the built-in tutorial. While reading it, you will quickly learn that most Emacs commands are accessed by holding the CRTL or ALT key and issuing some number of key presses. To open the tutorial, press and hold CTRL and type ‘h’. Release all the keys and type ‘t’ – the tutorial will then open, and it can teach you much more than I can!

In my next post, I will describe how I customised Emacs to help with Ruby and Rails development.

June 13, 2007 at 12:02 pm Leave a comment

Choosing an Editor

This tutorial is a bit out dated now.  I have created a new version of it over on my new blog which is an updated version of this one.

Over the last few months, I have started playing with Ruby and of course Rails, and I quickly became frustrated with the number files I had to keep opening and closing to manage all my models, views and controllers to name just a few. Until recently, I used any editor that was close to hand – Vim or Emacs in a terminal, Textpad on Windows and Textedit or emacs on the mac – needless to say I could not call myself a power user in any of them.

As many bloggers have said in the past, its worth learning one editor well, which is easy to say until another holy editor war breaks out leaving vim and emacs in opposite corners hurling insults at one another.

First of all, I decided to try out Textmate on my macbook. Its certainly a nice product, and I loved the code browsing pane for developing my Rails apps. I played with it for an evening, but always had that nagging feeling that the lack of portability would bite me sooner rather than later. I have no doubt that Textmate is a fantastic editor, but if I am going to invest my time in learning an editor really well, I need to be able to use it on Windows, in a terminal window and on a Mac. For now Textmate is confined to the Mac, so I ruled it out.

The emacs zealots create a lot of fuss about how great it is and to be honest, I like the way a total newbie can fire it up and actually get something typed immediately, unlike with Vim. I set about my mission, which was to see if emacs would satisfy my wish-list:

  • Provide a code browser pane like in Textmate
  • Ruby (and other languages) syntax highlighting
  • Automatic code indenting and automatic closing braces, quotes, if statements would be nice
  • Compile/Run code inside the editor
  • Do all of this on Windows, OS X and inside a decent terminal

Other things that would be nice nice to have:

  • Rails code snippets like in Textmate
  • Spell check as you type
  • Customisable color schemes to suit tired eyes!

Needless to say, emacs did not disappoint. After quite a bit reading and fiddling, I got it doing everything I wanted, moving me a small way up that power user ladder. In the next few articles, I will describe exactly what I did to get things working.

June 12, 2007 at 4:22 pm 5 comments


Feeds