Posts Tagged ‘character literal’

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.