Sunday, September 2, 2012

Showing scala code with orgmode

To code in scala I use either Eclipse or Intellij. To write my blog and do many other things I use Emacs and orgmode. To have syntax highlighting in orgmode, you need to have a mode for the language you are using (thanks to Bastien and Thorsten for their help). So to get a nicely colored code snippet in scala. First download scala-mode:

svn co http://lampsvn.epfl.ch/svn-repos/scala/scala-tool-support/trunk/src/emacs/ scala-mode

Then load it into emacs:

(add-to-list 'load-path "/path/to/scala-mode")
(require 'scala-mode-auto)

and now to create a scala snippets nicely exported, in orgmode:

#+begin_src scala
/** If given key is already in this map, returns associated value.
   *
   *  Otherwise, computes value from given expression `op`, stores with key
   *  in map and returns that value.
   *  @param  key the key to test
   *  @param  op  the computation yielding the value to associate with `key`, if
   *              `key` is previously unbound.
   *  @return     the value associated with key (either previously or as a result
   *              of executing the method).
   */
  def getOrElseUpdate(key: A, op: => B): B =
    get(key) match {
      case Some(v) => v
      case None => val d = op; this(key) = d; d
    }
#+end_src

will export like

/** If given key is already in this map, returns associated value.
   *
   *  Otherwise, computes value from given expression `op`, stores with key
   *  in map and returns that value.
   *  @param  key the key to test
   *  @param  op  the computation yielding the value to associate with `key`, if
   *              `key` is previously unbound.
   *  @return     the value associated with key (either previously or as a result
   *              of executing the method).
   */
  def getOrElseUpdate(key: A, op: => B): B =
    get(key) match {
      case Some(v) => v
      case None => val d = op; this(key) = d; d
    }

To create a blog post, I just export the file to html and copy/paste to blogger. You can see the difference with my previous post that was using the java highlighting.

No comments:

Post a Comment