开发者

Redefining ENTER key in Emacs

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?


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 :)


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))


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)))


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜