unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Indentation in python.el after "Enter"
@ 2007-07-11  7:35 Alexander Tsamutali
  2007-07-11  8:24 ` Eli Zaretskii
  2007-07-11  8:46 ` Ruslan Spivak
  0 siblings, 2 replies; 5+ messages in thread
From: Alexander Tsamutali @ 2007-07-11  7:35 UTC (permalink / raw)
  To: help-gnu-emacs

Hi, all!

I am novice emacs user and i'm using emacs to write python programs. I
used python-mode.el from http://python-mode.sourceforge.net and was
quite happy. A few days ago i updated emacs to 22.1 and read that the
default python.el mode is updated and recommended for use.
I removed python-mode.el and yes, python.el works good. The only thing
i miss form the old python-mode.el is that when i hit "Enter" on the
line which is indented by itself or opens the new block, emacs
maintains/creates indentation on the new line, so i don't need to hit
"Tab". For ex.:

def foo():
    a = 1 # hit "Enter"
    b = 2 # this line was already indented

if a == b: # hit "Enter"
    # cursor jumps here because i opened the block

When using python.el cursor always jumps to the first column and i
need to hit "Tab" to indent new line properly.

Can someone help me to make my emacs smarter in this area? :)

-- 
Alexander Tsamutali

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Indentation in python.el after "Enter"
  2007-07-11  7:35 Indentation in python.el after "Enter" Alexander Tsamutali
@ 2007-07-11  8:24 ` Eli Zaretskii
  2007-07-11  9:07   ` Alexander Tsamutali
  2007-07-11  8:46 ` Ruslan Spivak
  1 sibling, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2007-07-11  8:24 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Wed, 11 Jul 2007 12:35:11 +0500
> From: "Alexander Tsamutali" <astsmtl@gmail.com>
> 
> A few days ago i updated emacs to 22.1 and read that the
> default python.el mode is updated and recommended for use.
> I removed python-mode.el and yes, python.el works good. The only thing
> i miss form the old python-mode.el is that when i hit "Enter" on the
> line which is indented by itself or opens the new block, emacs
> maintains/creates indentation on the new line, so i don't need to hit
> "Tab".

If you press C-j instead of Enter, does python.el do what you want?
If so, all you need to do is bind the command that C-j runs to the
Enter (a.k.a. C-m) key in the python-mode-hook.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Indentation in python.el after "Enter"
  2007-07-11  7:35 Indentation in python.el after "Enter" Alexander Tsamutali
  2007-07-11  8:24 ` Eli Zaretskii
@ 2007-07-11  8:46 ` Ruslan Spivak
  1 sibling, 0 replies; 5+ messages in thread
From: Ruslan Spivak @ 2007-07-11  8:46 UTC (permalink / raw)
  To: help-gnu-emacs

"Alexander Tsamutali" <astsmtl@gmail.com> writes:

> Hi, all!
>
> I am novice emacs user and i'm using emacs to write python programs. I
> used python-mode.el from http://python-mode.sourceforge.net and was
> quite happy. A few days ago i updated emacs to 22.1 and read that the
> default python.el mode is updated and recommended for use.
> I removed python-mode.el and yes, python.el works good. The only thing
> i miss form the old python-mode.el is that when i hit "Enter" on the
> line which is indented by itself or opens the new block, emacs
> maintains/creates indentation on the new line, so i don't need to hit
> "Tab". For ex.:
>
> def foo():
>    a = 1 # hit "Enter"
>    b = 2 # this line was already indented
>
> if a == b: # hit "Enter"
>    # cursor jumps here because i opened the block
>
> When using python.el cursor always jumps to the first column and i
> need to hit "Tab" to indent new line properly.
>
> Can someone help me to make my emacs smarter in this area? :)
>
> -- 
> Alexander Tsamutali

Hi, Alexander

You can use something like this in your dot emacs file:

(defun my-python-hook ()
  (define-key python-mode-map "\C-m" 'newline-and-indent))

(add-hook 'python-mode-hook 'my-python-hook)

or a bit shorter version

(add-hook 'python-mode-hook
          '(lambda ()
              (define-key python-mode-map "\C-m" 'newline-and-indent)))

Regards,
Ruslan

-- 
Computers are useless.  They can only give you answers.
  - Pablo Picasso

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Indentation in python.el after "Enter"
  2007-07-11  8:24 ` Eli Zaretskii
@ 2007-07-11  9:07   ` Alexander Tsamutali
  2007-07-11  9:46     ` Eli Zaretskii
  0 siblings, 1 reply; 5+ messages in thread
From: Alexander Tsamutali @ 2007-07-11  9:07 UTC (permalink / raw)
  To: help-gnu-emacs

> If you press C-j instead of Enter, does python.el do what you want?
> If so, all you need to do is bind the command that C-j runs to the
> Enter (a.k.a. C-m) key in the python-mode-hook.

Thanks a lot! It works!

I added the following to my .emacs:

(add-hook 'python-mode-hook
          (lambda () (define-key python-mode-map "\C-m" 'newline-and-indent)))

Then i found solution to my problem on EmacsWiki
(http://www.emacswiki.org/cgi-bin/wiki/PythonMode)
They recommend to add the following to .emacs:

(define-key python-mode-map "\C-m" 'newline-and-indent)

I can guess that first variant modifies key bindings only in python
mode and second modifies key bindings globally. Am i right?

-- 
Alexander Tsamutali

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Indentation in python.el after "Enter"
  2007-07-11  9:07   ` Alexander Tsamutali
@ 2007-07-11  9:46     ` Eli Zaretskii
  0 siblings, 0 replies; 5+ messages in thread
From: Eli Zaretskii @ 2007-07-11  9:46 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Wed, 11 Jul 2007 14:07:13 +0500
> From: "Alexander Tsamutali" <astsmtl@gmail.com>
> 
> I added the following to my .emacs:
> 
> (add-hook 'python-mode-hook
>           (lambda () (define-key python-mode-map "\C-m" 'newline-and-indent)))
> 
> Then i found solution to my problem on EmacsWiki
> (http://www.emacswiki.org/cgi-bin/wiki/PythonMode)
> They recommend to add the following to .emacs:
> 
> (define-key python-mode-map "\C-m" 'newline-and-indent)
> 
> I can guess that first variant modifies key bindings only in python
> mode and second modifies key bindings globally. Am i right?

No, they both bind the Enter key in Python mode only, but the Wiki
solution requires that you load python.el in your .emacs, before you
bind the key, because otherwise python-mode-map is not defined, and
Emacs will barf.

The add-hook method is the better one, because it binds the key
whenever a buffer enters the Python mode, exactly when you need that.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-07-11  9:46 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-07-11  7:35 Indentation in python.el after "Enter" Alexander Tsamutali
2007-07-11  8:24 ` Eli Zaretskii
2007-07-11  9:07   ` Alexander Tsamutali
2007-07-11  9:46     ` Eli Zaretskii
2007-07-11  8:46 ` Ruslan Spivak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).