tags:

views:

93

answers:

4

I don't know elisp, but I'm trying to do something like the following:

(add-hook
 'scala-mode-hook
 (lambda ()
   (define-key scala-mode-map (kbd "RET") (lambda ()
                                            (scala-newline)
                                            (scala-indent-line)))))

Goal is to call the two functions each time I hit the ENTER key. How do I actually do this?

+2  A: 

In hook you can use local-set-key, for example

(add-hook 'scala-mode-hook
  (lambda ()
    (local-set-key [return] 
        (lambda ()
            (scala-newline)
            (scala-indent-line)))))

although, maybe it will be easier to use something like standard newline-and-indent?

(add-hook 'scala-mode-hook
  (lambda ()
    (local-set-key [return] 'newline-and-indent)))
Alex Ott
+7  A: 

You need an (interactive) form after the lambda in your define-key.

EDIT:

To be clear, the inner form should look like:

(lambda ()
  (interactive)
  (scala-newline)
  (scala-indent-line))
scottfrazer
+6  A: 

I do essentially this in so many modes that I've squashed them all together:

(mapcar (lambda (hooksym)
          (add-hook hooksym
                    (lambda ()
                      (local-set-key  (kbd "C-m") 'newline-and-indent)
                      )))
        '(
          clojure-mode-hook
          emacs-lisp-mode-hook
          erlang-mode-hook
          java-mode-hook
          js-mode-hook
          lisp-interaction-mode-hook
          lisp-mode-hook
          makefile-mode-hook
          nxml-mode-hook
          python-mode-hook
          ruby-mode-hook
          scheme-mode-hook
          sh-mode-hook
          ))

Just stick scala-mode-hook in there somewhere and it'll work for you too :)

offby1
A: 

Just type C-j it will call the newline-and-indent command and do exactly what you ask.

Jérôme Radix