dannyyuan
2/11/2007 5:09:00 PM
On Feb 8, 6:30 pm, james.d.mast...@gmail.com wrote:
> On Feb 8, 9:04 am, dannyy...@gmail.com wrote:
>
> > Hi,
> > I'm learning to use Emacs to program Ruby. The ruby-electric-mode
> > works well except that after I hit ENTER, my cursor will not be
> > automatically indented. For example, if I type "def foo" and then
> > ENTER, my cursor will be placed at the first column instead of being
> > properly indented, say, two spaces to the right of "def". Is there any
> > way to make theelectricmodedo auto indentation? Thanks!
>
> Here's what I picked up from a website about a month ago (and my left
> pinkey is much happier now):
>
> ;; Automatically indent the next line...
> (mapcar
> (lambda (mode)
> (let ((mode-hook (intern (concat (symbol-namemode) "-hook")))
> (mode-map (intern (concat (symbol-namemode) "-map"))))
> (add-hook mode-hook
> `(lambda nil
> (local-set-key (kbd "RET")
> (or (lookup-key ,mode-map "\C-j")
> (lookup-key global-map "\C-
> j")))))))
> '(ada-modec-modec++-modecperl-modeemacs-lisp-modejava-modehtml-mode
> lisp-modephp-moderuby-modesh-modesgml-mode))
Thanks, James! I'm happily using this code in my emacs now. I'm not
familar with Emacs Lisp, so I didn't run into a few road blocks. For
those who are not familiar with Emacs Lisp either, here are some tips
when you copy&paste the above code to your Emacs configuration file:
-- the "comma" in front of mode-map in the expression (lookup-
key ,mode-map "\C-j") is not a typo. I naively removed it, and was
supprised to get void-variable error. It turns out a comma in a list
means the symbol following the comma should be evaluated. It is
crucial here.The lambda expression in the add-hook function is not a
closure that contains the value of the mode-map, which is set by "let"
expression. Therefore, a comma in front of "mode-map" forces it to be
evaluated immediately so that it will retain the value of mode-map.
-- There are a few hard line-breaks inside a list or a string. They
should be removed.