Pragmatic Development Notes


Improved Emacs Customization

Filed under: Emacs,Ruby & RubyOnRails — Boško Ivanišević @ 14:44
Tags: , , , , ,

While I was writing first three articles about Emacs:

with somewhat clumsy titles because they are related to Linux too, I was changing my .emacs file. (Due to the WordPress restrictions I had to rename it. If you want to download it just right click on the link and save it somewhere under .emacs name. It is pure text file although it has .doc extension.)

I’ll briefly describe all changes I’ve made. First I’ve defined variable my-root. It points to the root folder where directory .emacs.d is. I needed this just because of frequent switches between Ubuntu and Vista. This change makes it easier to update Emacs configuration no matter whether it was changed on Ubuntu and copied to Windows or opposite. I only need to set my-root to path according to the system I work on.

I’ve also started Emacs server. With it only one instance of Emacs can be used, both on Linux or Windows, when various files are edited. If you want to be able to use it from Windows shell you’ll need Emacs Shell Extension Registry File. Right click on the link, save it with .reg extension and double click on saved file. It will add ‘Open in Emacs’ item in file pop-up menu on Windows.

Next change is related to font setting. It is well documented in .emacs file so I’ll not write about it here. Just will point out that if you want to find XFLD name of the font on Windows you’ll have to switch to *scratch* buffer and type:

(w32-select-font nil t)

After that just press C-j (Ctrl-j) at the end of the buffer and after you select font you want you’ll get the name you should use in your .emacs file.

I’ve also set default tab width to 2, enabled ANSI colors in the shell and set mode for editing .ini files.

Near the end of .emacs file there are several lines related to Cygwin. If you want to use Windows (GNU) version of Emacs under Cygwin you’ll have to uncomment these lines and to download cygwin-mount.el package.

Final change is something I really like, so I’ll describe it in more details. If you read previous articles you already know that I really enjoy working on Ruby and Ruby on Rails projects in Emacs. There was only one thing I was missing – navigation within the file. In Rails mode pressing M-S-down (Alt-Shift-down arrow) pops up menu for quick switch to files related to current context. For example if you do that while you have controller file opened you’ll get pop up menu with items to jump to Functional Test, Helper or views.

Pop up menu in Rails mode.

Pop up menu in Rails mode

What I needed is similar functionality but for navigation within the file which will make it easier to jump to function definition. Under IMENU menu item you can find all function definitions and you can switch to each of them but you must use mouse. There is one more way to do it with the mouse C-mouse 3 (pressing right mouse button while keeping Ctrl button pressed). (BTW two other useful shortcuts are C-mouse 1 (Ctrl and left mouse click) which opens buffers menu and C-mouse 2 (Ctrl and middle mouse click) which opens text properties menu.) Lets get back to navigation problem. Obviously I could use mouse to navigate through a file but I didn’t like it. I prefer to have keyboard shortcut for that. On the Web I’ve found are few examples related to Emacs pop-up menu and keyboard handling, and that was my starting point. I’ve modified these examples and made small menu-util.el package loaded at the end of .emacs file.

This package adds few things I really like. With C-M-down (Ctrl-Alt-down arrow) it will display major mode menu (which is the list of functions in Ruby file, for example).

Easier navigation in Emacs.

Easier navigation in Emacs

C-M-up (Ctrl-Alt-up arrow) pops up main menu so you will not need mouse any more.

Main menu as a pop up.

Main menu as pop up

Finally M-up will open completion pop-up menu while you are working on your code.

Completion menu in Emacs

Completion menu in Emacs

Since I really do not know Lisp I’m sure all these could be made much better. If any Lisp guru has an idea how to do that I’ll be very happy to hear suggestions. Until then I can live with what I’ve made and I hope you’ll find it useful too.


Emacs On Windows – Ruby & Ruby On Rails

Filed under: Emacs,Ruby & RubyOnRails — Boško Ivanišević @ 21:48
Tags: , , , ,

This is the last part of this small series about customizing Emacs. In first two parts I’ve wrote about basic customizations and adding various extensions. In this one I’m focusing on adjusting Emacs for Ruby and Ruby On Rails development. As usual, in order to do that we must get appropriate Lisp files.

On Windows systems they come with One-Click Ruby Installer. After installing it, all files needed for Ruby support in Emacs can be found in %RUBY_INSTALL_DIR%\doc\ruby\ruby-1.8.6\misc\ where %RUBY_INSTALL_DIR% is directory where you installed Ruby. If you are on Linux you can find necessary files directly in Ruby repository in trunk/misc folder (that’s how I did it on my Ubuntu 8.10 box). Copy ruby-electric.el, ruby-mode.el and rubydb3x.el to your includes folder. You can also copy rdebug.el but I’m using newer and better version so if you want to have really good Ruby debugging support wait just a little more, but only if you are using Emacs 22 and later.

Now it’s time to change .emacs file for Ruby support. First we will set up automatic activation of Ruby mode whenever any Ruby file, and HTML mode whenever any of Ruby On Rails (old or new) files are opened:

;; 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))
(setq auto-mode-alist (cons '(".html.erb$" . html-mode) auto-mode-alist))

Inserting closing parenthesis, 'end' keyword after 'def' etc. are cool features of lots of modern IDEs. Some developers like it, others not. If you are in the first group now it is time to enable it. For that purpose we copied electric.el file to our includes folder. I like ruby electric although there is one glitch in it I would like to be changed. After it inserts closing parenthesis if you type ‘)’ cursor will not be moved after it, but new ‘)’ character will be added. It would be much better if it moves cursor after inserted closing parenthesis. Anyway if you want to enable this feature use this part of Lisp code I’ve found on Stephen O’Donnell’s Software bits and pieces blog. By the way he has excellent posts about Emacs and Ruby.

(add-hook 'ruby-mode-hook
(add-hook 'local-write-file-hooks
(untabify (point-min) (point-max))
(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)
(require 'ruby-electric)
(ruby-electric-mode t)

Finally we came to the debugging part. As I mentioned earlier you can save rdebug.el from Ruby installation on Windows or from repository on Linux to includes folder and this is all you need if you are using earlier version of Emacs. But if you have Emacs 22 it is much better to download Lisp files directly from ruby-debug Subversion repository:

svn checkout svn://

Copy all lisp files to emacs-rdebug directory in plugins folder, and add following to your .emacs file:

;; Ruby debugging.
(add-to-list 'load-path "~/.emacs.d/plugins/emacs-rdebug")
(autoload 'rdebug "rdebug" "Ruby debugging support." t)
(global-set-key [f9] 'gud-step)
(global-set-key [f10] 'gud-next)
(global-set-key [f11] 'gud-cont)
(global-set-key "\C-c\C-d" 'rdebug)

Here is a screen shot of Emacs ruby debugging in action. As you can see Emacs really turns into IDE like application with this extension.

Ruby debugging in Emacs

Ruby debugging in Emacs

But what’s an IDE without integrated help for the programming language you’re working in? Of course you can go to the Web or read documentation locally but isn’t it better to have it in our favorite editor? Download ri-emacs, unpack it into includes folder and update your .emacs file:

;; ri emacs
(setq ri-ruby-script "~/.emacs.d/includes/ri-emacs.rb")
(autoload 'ri "~/.emacs.d/includes/ri-ruby.el" nil t)
(add-hook 'ruby-mode-hook (lambda ()
(local-set-key [f1] 'ri)
(local-set-key "\M-\C-i" 'ri-ruby-complete-symbol)
(local-set-key [f4] 'ri-ruby-show-args)

Above code bounds help to F1 key and adds shortcuts for completion based on ri documentation.

Packages snippet.el and finde-recursive.el are required for rails mode. Save downloaded files into includes folder and turn on their loading in Emacs.

(require 'snippet)
(require 'find-recursive)

Finally download emacs-rails. Unpack files to plugins folder and change .emacs file so Emacs switches to RoR minor automatically. Just be careful to add this after ECB is loaded. Otherwise Emacs will load incompatible speedbar.

(add-to-list 'load-path "~/.emacs.d/plugins/emacs-rails")
(require 'rails)

When you work with Ruby On Rails YAML minor mode can come in handy:

(require 'yaml-mode)
(add-to-list 'auto-mode-alist '("\\.yml$" . yaml-mode))
(add-hook 'yaml-mode-hook
'(lambda ()
(define-key yaml-mode-map "\C-m" 'newline-and-indent)))

When I work on Ruby or RoR project I don’t like to leave editor just to execute script and test the code. That’s why I added Mode compile to the list of my extensions:

(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)

MMM mode will enhance syntax coloring when you work on .erb files:

(add-to-list 'load-path "~/.emacs.d/plugins/mmm-mode-0.4.8")
(require 'mmm-mode)
(require 'mmm-auto)
(setq mmm-global-mode 'maybe)
(setq mmm-submode-decoration-level 2)
(set-face-background 'mmm-output-submode-face "#00688b")
(set-face-background 'mmm-code-submode-face "#104e8b")
(set-face-background 'mmm-comment-submode-face "DarkOliveGreen")
:submode ruby-mode
:match-face (("<%#" . mmm-comment-submode-face)
("<%=" . mmm-output-submode-face)
("<%" . mmm-code-submode-face))
:front ""
:insert ((?% erb-code nil @ "" @)
(?# erb-comment nil @ "" @)
(?= erb-expression nil @ "" @)))))

(add-hook 'html-mode-hook
(lambda ()
(local-set-key [f8] 'mmm-parse-buffer)
(setq mmm-classes '(erb-code))

Finally load speedbar and add files for Git support (if you use this wonderful SCM) which come with Git distribution. Just copy git.el and git-blame.el to includes folder.

(autoload 'speedbar "speedbar")

(require 'git)
(autoload 'git-blame-mode "git-blame"
"Minor mode for incremental blame for Git." t)

At the end I like to print out time needed for my .emacs file to be loaded:

(message "My .emacs loaded in %ds" (destructuring-bind (hi lo ms) (current-time)
(- (+ hi lo) (+ (first *emacs-load-start*) (second *emacs-load-start*)))))

The end. With this setup Emacs turns into powerful tool for Ruby and Ruby On Rails development. Give it a try and I'm sure after few days needed to get used to Emacs commands you will hardly leave it for some IDE or other editor. I didn't.


Emacs On Windows – Extensions

Filed under: Emacs,Ruby & RubyOnRails — Boško Ivanišević @ 6:14
Tags: , , , ,

In my previous post I gave basic Emacs customizations. What is really cool about this wonderful editor is that you can easily extend it. But don’t take this literally. It is easy if you know Lisp. Even then you might have to write lot of Lisp code depending on extension you want to make. Fortunately there are lot of extensions available on the net. Some are excellent, others can be better (just as any software, isn’t it) but you will certainly find almost anything you need. At least I did.

If you read my previous article you already know I like to keep things simple. That’s why I put all Emacs extensions in one folder. That way I can easily pack it and copy to another system. Actually that’s how I came up with final Emacs set up. I was changing it on Windows Vista, Ubuntu and Windows XP, moving all my extensions around all the time. Directory where I keep all extensions is in my home folder (see previous post for home folder on various systems) and is named .emacs.d. I’ve made two sub-folders in it – includes and plugins. The first one I use for simple extensions that consist of one or just a few files. The other one is for more complex extensions.

In order to be able to load extensions from includes directory add this to .emacs file:

;; Directory to put various el files.
(add-to-list 'load-path "~/.emacs.d/includes")

and don’t forget to adjust path according to your settings.

Let’s install the first extension. Download ASCII table put it in includes directory and add following lines at the end of your .emacs file:

;; Loading ascii-table
(require 'ascii-table)

You can fire up your Emacs and test plugin. Type M-x ascii-table and in a new buffer you’ll see a table with all ASCII characters, their decimal, octal and hexadecimal values. Useful thing.

Every modern editor has syntax highlighting. Emacs is not different. It handles it through various modes depending on a file currently edited. Actually Emacs’ modes do a little bit more then syntax highlighting, but that’s another story. Anyway, in order to support common modes add following to your .emacs file:

;; Loading collection of generic modes
(require 'generic-x)

File generic-x.el comes with Emacs distribution. It implements collection of generic modes. If you want to change which modes will be loaded you can do it by:

M-x customize-option RET generic-extras-enable-list RET

List of all available modes will be opened and you can select those you need.

If you don’t like original Emacs’ colors ColorTheme is right extension for you. Download it and unpack it in plugins folder. Current version is 6.6.0. As you already know you have to enable this plugin in .emacs file:

;; If you have own color scheme and don't like to
;; use emacs default I recommend to use
;; this package:
;; http:/
(add-to-list 'load-path "~/.emacs.d/plugins/color-theme-6.6.0")
(require 'color-theme)
(setq color-theme-is-global t)
;; This is my favorite color theme.

If you want to see what color themes are available just type:

M-x color-theme- TAB

Emacs will open new buffer with the list of all color themes. Just click on any of them to apply it. After you find the one you like just replace last line in above lines with the its name.

If you frequently edit same files you could find recentf extension useful. It keeps list of recently edited files. Once you install it you can access this list from File->Open Recent menu, or by typing some of the recentf commands which, as usual in Emacs, you can see by typing:

M-x recentf- TAB

By the way command completion is very powerful in Emacs. Just type few letters and hit TAB key and Emacs will either complete command for you or give you the list of possible candidates. Let’s go back to recent files list. You will enable it with following lines of Lisp code:

(require 'recentf)
(recentf-mode 1)

If you want to use Emacs for any development you can find Cedet plugin useful. Using Emacs for Ruby On Rails development will require this plugin. So go on download it and unpack it in your plugins directory. If you downloaded 1.0pre4 version add

(load-file "~/.emacs.d/plugins/cedet-1.0pre4/common/cedet.el")

;; Enabling various SEMANTIC minor modes. See
;; semantic/INSTALL for more ideas.
;; Select one of the following:

;; * This enables the database and idle reparse engines

;; * This enables some tools useful for coding, such as summary mode
;; imenu support, and the semantic navigator

(setq semanticdb-default-save-directory "~/.semantic.cache")

(setq semantic-load-turn-everything-on t)
(require 'semantic-load)

to your .emacs file. First line will enable cedet tools. Next two lines will turn on some necessary features. After that we prevent semantic to spread cache files all over the disk by putting them all in one common folder. Finally we turn on semantic extension.

All modern IDEs have one common (good) feature – code browsing. It really helps developers to quickly open files from their projects. Of course it comes with the price. Most of modern IDEs need lot of memory and they are much slower than Emacs. Don’t take me wrong. I use IDEs, but after using Eclipse with Aptana or NetBeans I’ve finished with Emacs for everyday Ruby and Ruby On Rails development. It doesn’t have fancy code completion, GUI debugging and some more things but you will see that Emacs’ support for all of that and even more is excellent and not so memory consumable.

So let’s take firs step to make Emacs IDE like. Grab ECB and unpack it into plugins folder. After that add these lines to your .emacs file:

;; This installs ecb - it is activated
;; with M-x ecb-activate
(add-to-list 'load-path "~/.emacs.d/plugins/ecb-2.32")
(require 'ecb-autoloads)

;; This puts ~/ruby folder as root for your Ruby projects
(setq ecb-source-path (quote ("~/ruby")))

Restart Emacs, type:

M-x ecb-activate

and enjoy your new IDE. It is not complete yet. We still have to add few things in order to get complete Ruby and Ruby On Rails support. But before we do that we’ll add some more cosmetics.

Grab tabbar.el from Emhacks, put it in includes folder and add:

(require 'tabbar)
(global-set-key [(control tab)] 'tabbar-forward)
(global-set-key [(control shift tab)] 'tabbar-backward)
(setq tabbar-cycling-scope "tabs")

This will enable tabs in Emacs. You can switch them with Ctrl-Tab (forward) and Ctrl-Shift-Tab (backward). Since Emacs groups buffers by the their, mode after last tab in one group it will switch to the first buffer in the next group or vice versa.

If you want to use Emacs for editing XML file you might find nXML mode useful. Unpack archive in plugins folder and enable it:

;; nxml
;; /usr/share/emacs/site-lisp/tcc-nxml-emacs:
;; Add these lines
;; to your .emacs to use nxml-mode. For documentation of
;; this mode, see
;; Add the nxml files to emacs's search
;; path for loading:
(setq load-path
(append load-path
;; Make sure nxml-mode can autoload
;(load "~/.emacs.d/plugins/nxml-mode-20041004/rng-auto.el")
; This is much better way of loading. File rng-auto.el will not be
; loaded until needed (for example by opening some .xml file)
(autoload 'nxml-mode "rng-auto.el" nil t)

;; Load nxml-mode for files ending in .xml, .xsl, .rng, .xhtml
(setq auto-mode-alist
(cons '("\\.\\(xml\\|xsl\\|rng\\|xhtml\\)\\'" . nxml-mode)

Recently I had to work a lot with JavaScript so I was looking for extension that will support it. I’ve found several but the one I’ve chosen is JS2. It is quite good, but if you find better one let me know. As usual download Lisp file and put it in includes folder and enable it in .emacs file:

;; JavaScript mode
(autoload 'js2-mode "js2-20080616a" nil t)
(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))

And finally mode which made possible to write all these in Emacs – Weblogger. Save Lisp file in includes folder and add:

(require 'weblogger)

to your .emacs file.

That’s all in this part. We have added few extensions and by now you should know now how to add those you need. In the next article I’ll finish setting up Emacs for Ruby and Ruby on Rails and in the final article I’ll write few remarks about Ruby debugging within Emacs.


Emacs On Windows – First Part

Filed under: Emacs,Ruby & RubyOnRails — Boško Ivanišević @ 2:23
Tags: , , , , ,

This is first post in small series of articles about Emacs. Although its title indicates that it is only about Windows most of it can be applied on Linux. Actually there are just a few Windows specific things in it. The goal is to get excellent environment for Ruby, Ruby on Rails development and tool for writing posts.

In order to set up Emacs you, naturally, have to download it first. You can download Xemacs, EmacsW32 or GNU Emacs. My favorite is GNU Emacs. There are several reasons for it. First it doesn’t come with the installer so I can always completely remove it if I don’t like it, without any fear that some files will be left as is so usual for the most of the applications on Windows. Second, on GNU Emacs download site I can always download latest version as soon as it is released. I do not have to wait patches to be applied or some installers to be made. Simple and clean.

Installing Emacs on Linux depends on the flavor you use. Ubuntu is the one I like the best. Installing Emacs on Ubuntu is quite easy. If you want official release just use:

sudo apt-get install emacs22

On the other hand if you want to see what new version will look like do:

sudo apt-get install emacs-snapshot

By the way I’m writing this post in snapshot version ( on Ubuntu 8.10.

I like to keep all good and useful things grouped, so I keep them in C:\Utils folder. After downloading zip archive I’ve unpacked Emacs in C:\Utils\emacs-22.3 folder. Of course, you can unpack it wherever you want. Anyway all customizations and additional packages will be in separate folder.

One small digression. I use Cygwin a lot on Windows. Unfortunately current official version of Emacs on Cygwin is 21.3. It is quite old version and some of Lisp add-ins I use do not work with it. Version 22.1.3 which is marked as experimental always hangs both on Vista and XP and I could not use it. So I’ve decided to use same version on Cygwin. Some of the customizations will be Cygwin specific but I’ll make a note so you can omit if you don’t need them.

Now when Emacs is up and running it is time to customize it. First little introduction. During start up Emacs reads .emacs file from home folder. On Linux everybody knows what home folder is (/home/[user_name]), but on Windows home folder is different if you are on Vista (it has everything different, unfortunately) or on some earlier version. On Vista, home folder is in C:\Users\[user_name]\AppData\Roaming, and on XP it is in C:\Documents And Settings\[user_name]\Application Data. If you don’t like where Emacs searches for its start up file you can always change it by setting HOME environment variable to something else what is more suitable for you. I do not do that because I try to keep things as they are by default because it is easier for me to restore complete system if I need it.

Finally here is the first part of my .emacs file:

; Library with common Lisp extensions for Emacs.
(require 'cl)

; Needed to see how fast Emacs loads.
; Loading time is printed at the and of the
; execution of .emacs file.
(defvar *emacs-load-start* (current-time))

; I really like this font. I also tried Monaco which you can
; see on lot of Railscasts but I couldn't find the one which
; supports Serbian Cyrillic and Latin letters.
(set-default-font "Bitstream Vera Sans Mono-9")

; All customizations will be saved in separate file. This way
; it is easy to remove customizations and keep this file clean.
(setq custom-file "~/.emacs-custom.el")

; Now load all customized values.
(load custom-file 'noerror)

; This should allegedly speed up Emacs starting by preventing
; some requests from the window manager back to the Emacs. Frankly
; speaking I didn't notice some speed up but I still keep it:(
(modify-frame-parameters nil '((wait-for-wm . nil)))

;Allows syntax highlighting to work, among other things
(global-font-lock-mode 1)

; Sets initial window position
(set-frame-position (selected-frame) 70 70)

; Sets initial window size to 85 columns and 47 rows
(set-frame-size (selected-frame) 85 47)

; Makes last line ends in carriage return
(setq requre-final-newline t)

; Sets Ctrl-x / key combination for easy commenting
; out of selected lines.
(global-set-key "\C-x/" 'comment-or-uncomment-region)

; Allow resizing of the mini-buffer when necessary
(setq resize-minibuffer-mode t)

; I know lot of you will criticize me for this but all this
; years spent on Windows platform made me to do this:(
; Turns Windows-like selection mode.

; Auto magically read compressed files
(auto-compression-mode 1)

; Set standard indent to 2 rather then 4
(setq standard-indent 2)

; This tells Emacs to create backup files.
(setq make-backup-files t)

; And this will enable versioning with default values.
(setq version-control t)

; Remove annoying message about deleting excess backup of .recentf
; which is list of recent files used and will be explained in next
; post.
(setq delete-old-versions t)

; Finally do not spread backups all over the disk.
; Just save all backup files in this directory.
(setq backup-directory-alist (quote ((".*" . "~/.emacs_backups/"))))

That’s it for now. This will make your Emacs a little bit better with nicer font and with backup files grouped in one directory.

In the next post we’ll add some useful extensions.

Blog at