Skip to content

Instantly share code, notes, and snippets.

@stew
Created September 3, 2013 14:44
Show Gist options
  • Save stew/6424904 to your computer and use it in GitHub Desktop.
Save stew/6424904 to your computer and use it in GitHub Desktop.

Revisions

  1. stew created this gist Sep 3, 2013.
    68 changes: 68 additions & 0 deletions scala.el
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    ; scala-mode2 found here: https://github.com/hvesalai/scala-mode2 is much better than the scala mode that comes with scalac
    (add-to-list 'load-path "~/.emacs.d/scala-mode2/")
    (require 'scala-mode)

    ; http://www.emacswiki.org/emacs/RainbowDelimiters
    (add-hook 'scala-mode-hook 'rainbow-delimiters-mode)

    ; I get ensime from git:
    ; https://github.com/aemoncannon/ensime
    ; then run `sbt stage` inside the ensime source directory,
    ; then copy the resulting dist directory to ~/.emacs.d/ensime
    (add-to-list 'load-path "~/.emacs.d/ensime/elisp")
    (add-hook 'scala-mode-hook 'ensime-scala-mode-hook)

    ; help ensime find documentation urls:
    (defun make-scalaz-doc-url (type &optional member)
    (ensime-make-scala-doc-url-helper
    "https://scalazproject.ci.cloudbees.com/job/nightly/lastStableBuild/artifact/target/scala-2.9.2/unidoc/" type member))

    (add-to-list 'ensime-doc-lookup-map '("^scalaz\\." . make-scalaz-doc-url))

    (defun make-scalatra-doc-url (type &optional member)
    (ensime-make-scala-doc-url-helper
    "http://scalatra.org/2.2/api/" type member))

    (add-to-list 'ensime-doc-lookup-map '("^org\\.scalatra\\." . make-scalatra-doc-url))

    ; if you run ensime a bunch, this can be unfortunetely handy
    (defun killall-java ()
    (interactive)
    (shell-command "killall java"))

    (global-set-key (kbd "C-c C-v K") 'killall-java)

    ; automatically make arrows fancy
    (defun scala-right-arrow ()
    (interactive)
    (cond ((looking-back "=")
    (backward-delete-char 1) (insert ""))
    ((looking-back "-")
    (backward-delete-char 1) (insert ""))
    (t (insert ">"))))

    (defun scala-left-arrow ()
    (interactive)
    (if (looking-back "<")
    (progn (backward-delete-char 1)
    (insert ""))
    (insert "-")))

    ; this makes C-M-a do a better job of finding the beginning of a definition
    (defun scala-beginning-of-defun (&optional arg)
    (re-search-backward "\\(\\(case\\s*\\)?class\\|def\\|object\\|trait\\)" nil t arg))

    ; this makes C-M-e do a better job of finding the beginning of a definition
    (defun scala-end-of-defun (&optional arg)
    (scala-beginning-of-defun)
    (goto-char (- (search-forward "{") 1))
    (forward-sexp))

    (add-hook 'scala-mode-hook
    '(lambda ()
    (local-set-key (kbd "-") 'scala-left-arrow)
    (local-set-key (kbd ">") 'scala-right-arrow)
    (set (make-local-variable 'beginning-of-defun-function) 'scala-beginning-of-defun)
    (set (make-local-variable 'end-of-defun-function) 'scala-end-of-defun)
    (hs-minor-mode)))