auto indentation in ruby electric mode

D

dannyyuan

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!
 
X

Xavier Noria

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
 
D

dannyyuan

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!
 
J

james.d.masters

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

Xavier Noria

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

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
 
D

dannyyuan

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top