Question about emacs indentation

S

Sam Kong

As I got more and more inclined to Ruby, I decided to switch to Linux
from Windows.
Most people in this group seem to be using Linux and I felt that I was
an outsider...:)

I'm trying to get familiar with Emacs.
Now I get syntax coloring for Ruby.
But one thing that I can't get is indentation.

Under Windows, I used UntraEdit or EditPlus and I expect the similar
behaviors.

I like tab-size to be 4.
I don't want my tabs to be replaced with spaces.
And of course, auto-indentation.
Also, if I hit backspace, it should act like a tab key backward if the
removed character is a tab.

I tried to modify my .emacs file but I couldn't get all the things I
want.

class C
def f
"Hello"

Now I want to type '<backspace>end' to make it like

class C
def f
"Hello"
end

But I get either of the two depending on .emacs.
1. When I hit <backspace>, the cursor goes to the first column(begin of
the line). And I hit <tab> and the cursor moves 8 columns instead of 4
even if tab size is 4.
2. When I hit <backspace>, the cursor moves only one column back and I
have to hit it 4 times. This is better than the first situation but not
what I exactly want.

Here's part of my .emacs.

-----
;;To use ruby-mode
(autoload 'ruby-mode "ruby-mode" "Load ruby-mode")
(add-to-list 'auto-mode-alist '("\\.rb\\'" . ruby-mode))
(setq interpreter-mode-alist (append '(("ruby" . ruby-mode))
interpreter-mode-alist))

;;To set inf-ruby key definition
(autoload 'run-ruby "inf-ruby" "Run an inferior Ruby process")
(autoload 'inf-ruby-keys "inf-ruby" "Set local key defs for inf-ruby
in ruby-mode")
(add-hook 'ruby-mode-hook
'(lambda ()
(inf-ruby-keys)
(setq ruby-indent-level 4)
))

(setq ruby-mode-hook
(lambda ()
(setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
64 68 72 76 80))
(define-key ruby-mode-map "\r" 'newline-and-indent)
(define-key ruby-mode-map "\M-\C-h" 'backward-kill-word)
(setq indent-tabs-mode nil)
(setq c-tab-always-indent nil)
(abbrev-mode t)
(turn-on-font-lock)
(setq ruby-indent-level 4)
)
)

-----

Can you help me?
Thanks.

Sam
 
Z

zerohalo

I don't use emacs, but I've found JEdit to be quite good for Ruby -
syntax highlighting, indentation, and even ruby documentation (if you
install the Ruby plugin for it).
 
K

Konstantin Levinski

I think I can help with explanation of why emacs behaves as it does,
and maybe you'd agree that it makes sense.
<tab> in emacs/ruby means (ruby-indent-command), which parses vicinity
of cursor and figures out correct identation. that is, it is not "add a
tab" but "format this line".
so to get desired result, you could just use 'end<tab>'
or call (indent-region) - Ctl-Alt-\ - to indent the whole thing in one
go later.

This way you dont need to care about formatting at all, it's done
correctly automatically.
 
J

James Kim

zerohalo said:
I don't use emacs, but I've found JEdit to be quite good for Ruby -
syntax highlighting, indentation, and even ruby documentation (if you
install the Ruby plugin for it).

Hi, zerohalo.

I heard that JEdit is running on JAVA platform. However, Ruby is not
developed by Java language as like Jython. I mean that there is no
Jython like tool for Ruby, noting that Jython is Java-based interpreter
perfectly compatible with Python (CPython) in terms of grammer. Thus, is
it good idea to use JEdit for Ruby editing although it is not based on Java?

-James (^o^)
 
S

Sam Kong

Hi, Konstantin!

Konstantin said:
I think I can help with explanation of why emacs behaves as it does,
and maybe you'd agree that it makes sense.

Now I understand it.
But it's hard to change habit.
Do you think the emacs way is better?
<tab> in emacs/ruby means (ruby-indent-command), which parses vicinity
of cursor and figures out correct identation. that is, it is not "add a
tab" but "format this line".
so to get desired result, you could just use 'end<tab>'
or call (indent-region) - Ctl-Alt-\ - to indent the whole thing in one
go later.

This way you dont need to care about formatting at all, it's done
correctly automatically.

How can I keep tabs instead of spaces?

Thanks.

Sam
 
G

George Ogata

Sam Kong said:
Hi, Konstantin!



Now I understand it.
But it's hard to change habit.
Do you think the emacs way is better?

I got used to using C-j instead of RETs and TABs. Give it a bash.
How can I keep tabs instead of spaces?

For all modes by default:

(set-default 'indent-tabs-mode t)

For just ruby-mode:

(add-hook 'ruby-mode-hook (lambda () (setq indent-tabs-mode t)))
 
D

David Vallner

George said:
I got used to using C-j instead of RETs and TABs. Give it a bash.
Eugh, my pinky finger hurts enough from coding on a German keyboard from
reaching to AltGr all the time, reaching for Ctrl for each newline would
definately give me CTS ;P You can always hack ruby-mode.el and map
RUBY-REINDENT-NEWLINE-AND-INDENT (word order subject to change) to RET -
I managed even as an utter elisp newbie. Works like a charm.

David Vallner
 
G

George Ogata

David Vallner said:
Eugh, my pinky finger hurts enough from coding on a German keyboard from
reaching to AltGr all the time, reaching for Ctrl for each newline would
definately give me CTS ;P You can always hack ruby-mode.el and map
RUBY-REINDENT-NEWLINE-AND-INDENT (word order subject to change) to RET -
I managed even as an utter elisp newbie. Works like a charm.

Heh. I'm surprised you'd put up with emacs at all if the control-key
proliferosity bothered you that much. I C-[fbpnaejod...] so often it
feels quite natural.

But you don't need to hack ruby-mode.el; just hook it in:

(add-hook 'ruby-mode-hook
(lambda ()
(define-key ruby-mode-map [return]
'ruby-reindent-then-newline-and-indent)))
 
K

Konstantin Levinski

well, I think emacs way of autoformatting instead of 'fixed identation
levels' is better, as it is _automates_ things. (and does not get in
the way in the process /well, most of the time/ )
you can bind 'ruby-reindent-then-newline-and-indent to enter (as was
already proposed) and forget about formatting forever.
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top