Thursday, April 29, 2010

Glorious Hack: pretty-mode for Emacs

Today I was lamenting the lack of unicode identifiers and strings in Standard ML.  I asked on the #emacs irc channel how hard it would be to display the string "alpha" as the glyph α, for example.  User `jlf' pointed me to pretty-mode for emacs from Arthur Danskin, a student at Johns Hopkins:

www.emacswiki.org/emacs/pretty-mode.el

This is a beautiful hack.  It uses font-lock mode to render strings as other strings rather than just coloring the text in the buffer.  It is easy to add your own unicode characters to display, and the code will compile because only the view is modified.  It's possible to use two different codes for α, one for source code identifiers that expands to a legal identifier in the language and another that expands to a unicode sequence that will print as the unicode character.  This is handy for a language like Standard ML (MLton) where unicode is allowed in neither identifiers nor strings.  For instance


val forall =  "\226\136\128"

prints as

val ∀ = "∀"

3 comments:

  1. If you're interested in customizing pretty-mode it is very easy. You're welcome to check out my copy if you like, too.

    ReplyDelete
  2. Hi Grant. Please send me your version when you get a moment so I can try yours as well. seanmcl at gmail. Thanks!

    ReplyDelete
  3. I just mailed it.

    In emacs 23.3.1 the right-halve of the characters get clipped off though.

    ReplyDelete