Forum: Ruby auto indentation in ruby electric mode

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5b9fd76e117a85b70c9fb9a53e97121a?d=identicon&s=25 unknown (Guest)
on 2007-02-08 18:06
(Received via mailing list)
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 the electric mode do auto indentation? Thanks!
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2007-02-08 19:51
(Received via mailing list)
On Feb 8, 2007, at 6:05 PM, dannyyuan@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 the electric mode do auto indentation? Thanks!

By default, in Emacs that is accomplished with C-j (newline-and-
indent). You may map RET to C-j if that's more natural for you.

-- fxn
5b9fd76e117a85b70c9fb9a53e97121a?d=identicon&s=25 unknown (Guest)
on 2007-02-08 21:16
(Received via mailing list)
On Feb 8, 1:50 pm, Xavier Noria <f...@hashref.com> wrote:
> By default, in Emacs that is accomplished with C-j (newline-and-
> indent). You may map RET to C-j if that's more natural for you.
>
> -- fxn

Neat! Thank you very much, Xavier!
E92d09efe27bf50a87967c533fae247a?d=identicon&s=25 unknown (Guest)
on 2007-02-09 00:35
(Received via mailing list)
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 the electric mode do 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-name mode) "-hook")))
         (mode-map  (intern (concat (symbol-name mode) "-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-mode c-mode c++-mode cperl-mode emacs-lisp-mode java-mode html-
mode
            lisp-mode php-mode ruby-mode sh-mode sgml-mode))
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2007-02-09 08:43
(Received via mailing list)
On Feb 9, 2007, at 12:35 AM, james.d.masters@gmail.com wrote:

> Here's what I picked up from a website about a month ago (and my left
>                                  (or (lookup-key ,mode-map "\C-j")
>                                      (lookup-key global-map "\C-
> j")))))))
>  '(ada-mode c-mode c++-mode cperl-mode emacs-lisp-mode java-mode html-
> mode
>             lisp-mode php-mode ruby-mode sh-mode sgml-mode))

Just in case, if mapping RET globally is fine just put this single
line instead:

   (global-set-key (kbd "RET") 'newline-and-indent)

Less specific but much simple.

-- fxn
5b9fd76e117a85b70c9fb9a53e97121a?d=identicon&s=25 unknown (Guest)
on 2007-02-11 18:11
(Received via mailing list)
On Feb 8, 6:30 pm, james.d.mast...@gmail.com wrote:
> Here's what I picked up from a website about a month ago (and my left
>                                  (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.
This topic is locked and can not be replied to.