Posts Tagged ‘ruby-mode’

imenu for Ruby

Tuesday, April 22nd, 2008

If you use ruby-mode, you need to add

(imenu-add-to-menubar "iRuby")

to your .emacs right now.

This adds a menu to the menubar, titled “iRuby”, which lists all the classes and methods.  When you select one, it jumps to that point in the file. If you are a keyboard junkie, you can run M-x imenu and get keyboard completion on any of these.

The downsides of this are:

  • it doesn’t update automatically: you need to choose the “*Rescan*” menuitem
  • it does only simple regexp-based parsing, nests instead of putting more than 25 items in the menu, and various other slightly-annoying things

I started a project, iruby-mode, to be what I was looking for, but since I figured out a 1-line way to get me 85% or 90% of the way there, I have much less incentive to work on this.

ruby-mode.el regexp bug

Tuesday, April 22nd, 2008

ruby-mode.el has a nasty bug: sometimes it won’t recognize the end of a string, so you end up with half your file colored the weird string-color.  Of course, it’s very annoying.

Today, I discovered the cause of this: it happens when your string ends with a “?” and there’s a “‘” earlier in the string.  ruby-mode thinks the ?” is a character literal.  You know, that feature of Ruby which has been used exactly twice in the history of Ruby programming.  *facepalm*

The fix is easy, assuming you never use character literals: open up ruby-mode.el and scroll down to about line 1019:

	  ;; $' $" $` .... are variables
	  ;; ?' ?" ?` are ascii codes
	  ("\\(^\\|[^\\\\]\\)\\(\\\\\\\\\\)*[?$]\\([#\"'`]\\)" 3 (1 . nil))
	  ;; regexps
	  ("\\(^\\|[=(,~?:;]\\|\\(^\\|\\s \\)\\(if\\|elsif\\|unless\\|while\\|until\\|when\\|and\\|or\\|&&\\|||\\)\\|g?sub!?\\|scan\\|split!?\\)\\s *\\(/\\)[^/\n\\\\]*\\(\\\\.[^/\n\\\\]*\\)*\\(/\\)"

Comment out the line that ends (1 . nil)). Recompile the .elc, if you have such a file.

If anybody wants to come up with a fix for this that works for both string and character literals, be my guest. I’m not going to spend any time tweaking 15 lines of Emacs Lisp regexps for some feature I never use.

Thanks to Phil Hagelberg for inspiring me to fix this.