unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Disabling automatic reindentation of the current line after a newline
@ 2022-10-22 15:18 Jean Abou Samra
  2022-10-24  2:44 ` Emanuel Berg
  0 siblings, 1 reply; 14+ messages in thread
From: Jean Abou Samra @ 2022-10-22 15:18 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

Suppose I am in Lisp mode and I write this:

(defun f (x)
     (g x))

Note the extra spaces compared to standard indentation. If, after
writing the last parenthesis, I press RET, Emacs automatically
reindents the second line, changing the code to this:

(defun f (x)
   (g x))


The same happens with other major modes.

I am aware that I can turn this off with M-x electric-indent-mode.
However, that also disables any automatic indentation for the next
line when I press RET. What I'd like is for Emacs to auto-insert
the number of spaces it deems necessary for the new, still blank
line whenever I press RET, without also reindenting the previous
line, where I might have changed the indentation if Emacs made
a mistake (e.g., if the major mode I am using is not 100% accurate,
or if I have a slightly different style for which no option exists
in that mode).

Is this possible?

Thanks,
Jean






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

* Re: Disabling automatic reindentation of the current line after a newline
  2022-10-22 15:18 Disabling automatic reindentation of the current line after a newline Jean Abou Samra
@ 2022-10-24  2:44 ` Emanuel Berg
  2022-10-24 15:29   ` Jean Abou Samra
  0 siblings, 1 reply; 14+ messages in thread
From: Emanuel Berg @ 2022-10-24  2:44 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Abou Samra wrote:

> where I might have changed the indentation if Emacs made
> a mistake (e.g., if the major mode I am using is not 100%
> accurate, or if I have a slightly different style for which
> no option exists in that mode).

Emacs doesn't make mistakes, it is always 100% accurate
according to the programming which defines it's bahvior.

If there is a mistake, it's on the behalf of the programmers
and they will be delighted to fix all such bugs.

If they think the behavior is good, and you disagree, you can
change the code or don't use it, or don't use it temporarily
perhaps, also on your own computer where you are unrestricted
to do whatever you want.

If you describe the exact situation we can help you with
that ...

> Is this possible?

Can you show an example what you want should happen ...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Disabling automatic reindentation of the current line after a newline
@ 2022-10-24  9:16 Jean Abou Samra
  2022-10-24 17:58 ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 14+ messages in thread
From: Jean Abou Samra @ 2022-10-24  9:16 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

Suppose I am in Lisp mode and I write this:

(defun f (x)
          (g x))

Note the extra spaces compared to standard indentation. If, after
writing the last parenthesis, I press RET, Emacs automatically
reindents the second line, changing the code to this:

(defun f (x)
  (g x))


The same happens with other major modes.

I am aware that I can turn this off with M-x electric-indent-mode.
However, that also disables any automatic indentation for the next
line when I press RET. What I'd like is for Emacs to auto-insert
the number of spaces it deems necessary for the new, still blank
line whenever I press RET, without also reindenting the previous
line, where I might have changed the indentation if Emacs made
a mistake (e.g., if the major mode I am using is not 100% accurate,
or if I have a slightly different style for which no option exists
in that mode).

Is this possible?

Thanks,
Jean





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

* Re: Disabling automatic reindentation of the current line after a newline
  2022-10-24  2:44 ` Emanuel Berg
@ 2022-10-24 15:29   ` Jean Abou Samra
  2022-10-24 21:12     ` Emanuel Berg
  0 siblings, 1 reply; 14+ messages in thread
From: Jean Abou Samra @ 2022-10-24 15:29 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: incal

Hi,

Thanks for replying.


Le 24/10/2022 à 04:44, Emanuel Berg a écrit :
> Emacs doesn't make mistakes, it is always 100% accurate
> according to the programming which defines it's bahvior.
>
> If there is a mistake, it's on the behalf of the programmers
> and they will be delighted to fix all such bugs.
>
> If they think the behavior is good, and you disagree, you can
> change the code or don't use it, or don't use it temporarily
> perhaps, also on your own computer where you are unrestricted
> to do whatever you want.
>
> If you describe the exact situation we can help you with
> that ...
>
>> Is this possible?
> Can you show an example what you want should happen ...



I have found myself wanting this in several different contexts.

a) When writing Scheme code, sometimes I define a custom macro
that should be indented specially. I know I could look up
how to define a custom indentation format for that macro and
use it in the local variables, but for just one call site
it's handier to just redo it manually.

b) LilyPond contains a C++ file formatted like this:


namespace Lily
{
extern Scm_module module;
typedef Module_variable<module> Variable;

extern Variable all_music_font_encodings;
extern Variable alterations_in_key;
extern Variable backend_testing;
extern Variable bar_glyph_alist;
extern Variable bar_line_calc_glyph_name_for_direction;
extern Variable base_length;


When I press RET to add an entry in that list, Emacs
inserts two spaces, according to the style it has learnt,
so I remove them ... until I remember that the moment
I insert the ending semicolon, the spaces are back.
This annoys me a bit because I removed the spaces manually,
I don't want Emacs to stand in the way.


c) At some point, the Coq mode provided by Proof General
completely removed all indentation on the previous line when
inserting a newline, at least in recent Emacs versions, but
I see that it seems to have been fixed now.


In summary, while the major mode tries to do its best,
sometimes it has a glitch or doesn't exactly do what I want,
and I'd like a handier way to correct those mistakes.

Best,
Jean




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

* Re: Disabling automatic reindentation of the current line after a newline
  2022-10-24  9:16 Jean Abou Samra
@ 2022-10-24 17:58 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-10-25 10:23   ` Anders Munch
  2022-10-26 21:55   ` Jean Abou Samra
  0 siblings, 2 replies; 14+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-10-24 17:58 UTC (permalink / raw)
  To: help-gnu-emacs

> line when I press RET. What I'd like is for Emacs to auto-insert the
> number of spaces it deems necessary for the new, still blank line
> whenever I press RET, without also reindenting the previous line,

Set `electric-indent-inhibit` to t.

Tho if it's only for the cases where Emacs's choice of indentation is
wrong, I think a better course of action might be:

- Use `C-q C-j` instead of RET the rare times it's necessary.
- If it's not rare (or if it's an indentation bug rather than
  a question of style), submit a bug report to the major
  mode's maintainers.

For modes using SMIE you can also add a comment with the word
"fixindent" in it to tell the auto-indentation that it shouldn't touch
this line (this is also obeyed by `indent-region`, etc...).
It's not a very elegant solution, admittedly.


        Stefan




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

* Re: Disabling automatic reindentation of the current line after a newline
  2022-10-24 15:29   ` Jean Abou Samra
@ 2022-10-24 21:12     ` Emanuel Berg
  0 siblings, 0 replies; 14+ messages in thread
From: Emanuel Berg @ 2022-10-24 21:12 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Abou Samra wrote:

> In summary, while the major mode tries to do its best,
> sometimes it has a glitch or doesn't exactly do what I want,

Those are or can be two different things, if they are glitches
they can and should be corrected, if you OTOH disagree with
how they work and can't convince the maintainer of the mode
you're right you can either do it manually _or_ change the
mode yourself, however if you do (change it) and the
maintainer is willing perhaps that or those changes can still
be integrated in the mode, and an option will enable it/them
while still doing everything else the old way ...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Disabling automatic reindentation of the current line after a newline
  2022-10-24 17:58 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-10-25 10:23   ` Anders Munch
  2022-10-25 13:03     ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-10-26 21:55   ` Jean Abou Samra
  1 sibling, 1 reply; 14+ messages in thread
From: Anders Munch @ 2022-10-25 10:23 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org

Stefan Monnier wrote:
>> line when I press RET. What I'd like is for Emacs to auto-insert the 
>> number of spaces it deems necessary for the new, still blank line 
>> whenever I press RET, without also reindenting the previous line,
>
> Set `electric-indent-inhibit` to t.

How do you do that when electric-indent-inhibit automatically becomes
buffer-local when set?

> Tho if it's only for the cases where Emacs's choice of indentation is wrong [..]

I'm with Jean. I find it an unpleasant experience when keybindings that I expect
to insert text to the right of the cursor, suddenly also changes text to the
left of the cursor.

I do use C-q C-j, but I'd like to reserve that for when I don't want the next
line indented.  TAB usually works fine for reindenting the current line.

Is there any reason that having electric-indent-inhibit set to t globally is not
viable? 

regards, Anders



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

* Re: Disabling automatic reindentation of the current line after a newline
  2022-10-25 10:23   ` Anders Munch
@ 2022-10-25 13:03     ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-10-25 13:39       ` Anders Munch
  0 siblings, 1 reply; 14+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-10-25 13:03 UTC (permalink / raw)
  To: help-gnu-emacs

>> Set `electric-indent-inhibit` to t.
>
> How do you do that when electric-indent-inhibit automatically becomes
> buffer-local when set?

Same as what you do for all the vars that share this characteristic,
like `indent-tabs-mode`.  I.e. either set the var from a mode hook or
use `setq-default`.


        Stefan




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

* Re: Disabling automatic reindentation of the current line after a newline
  2022-10-25 13:03     ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-10-25 13:39       ` Anders Munch
  2022-10-26  2:59         ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 14+ messages in thread
From: Anders Munch @ 2022-10-25 13:39 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org

Stefan Monnier wrote:
>> How do you do that when electric-indent-inhibit automatically becomes 
>> buffer-local when set?
>
> Same as what you do for all the vars that share this characteristic, like `indent-tabs-mode`.  I.e. either set the var from a mode hook or use `setq-default`.

setq-default is the missing piece that I did not know. Thank you.

regards, Anders



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

* Re: Disabling automatic reindentation of the current line after a newline
  2022-10-25 13:39       ` Anders Munch
@ 2022-10-26  2:59         ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-10-26  6:04           ` Emanuel Berg
  0 siblings, 1 reply; 14+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-10-26  2:59 UTC (permalink / raw)
  To: help-gnu-emacs

Anders Munch [2022-10-25 13:39:06] wrote:
> Stefan Monnier wrote:
>>> How do you do that when electric-indent-inhibit automatically becomes 
>>> buffer-local when set?
>>
>> Same as what you do for all the vars that share this characteristic, like
>> `indent-tabs-mode`.  I.e. either set the var from a mode hook or use
>> `setq-default`.
> setq-default is the missing piece that I did not know. Thank you.

To some extent, I think `setq` should basically never be used other than on
lexical variables: for non-lexical variables, we should either use
`setq-default` or `setq-local` but never this `setq` which does sometime
one thing and sometimes the other.


        Stefan




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

* Re: Disabling automatic reindentation of the current line after a newline
  2022-10-26  2:59         ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-10-26  6:04           ` Emanuel Berg
  0 siblings, 0 replies; 14+ messages in thread
From: Emanuel Berg @ 2022-10-26  6:04 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier via Users list for the GNU Emacs text editor wrote:

> To some extent, I think `setq` should basically

Note the word "basically" ...

> never be used other than on lexical variables: for
> non-lexical variables, we should either use `setq-default`
> or `setq-local` but never this `setq` which does sometime
> one thing and sometimes the other.

Because, most often it works like this, `setq' sets the
default/global value if that is set, but the _local_ if that
or both are set, and if neither are, it sets the default -
unless the variable is defined with DEFVAR_PER_BUFFER in C or
`make-local-variable' in Elisp, that means it "Automatically
becomes buffer-local when set" so then setq will be like
`setq-local' and not `setq-default' all tho none are
set. Phew!

Yes, most often ... but not always?

; don't use `setq' here, @#$&%!
(setq-local lexical-binding nil)

; because `lexical-binding' is dynamic/special, i.e. the variable itself
(special-variable-p 'lexical-binding)

 ; this function relies on a dynamic and/or special variable `n'
(defun use-var ()
  n)

; formal parameters are dynamic under dynabound
(defun lexfun (n)
  (setq n 1)
  (special-variable-p 'n) ; but not special since they are not global
  (use-var) )

;; (lexfun 2) ; 1

(special-variable-p 'n) ; nil

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Disabling automatic reindentation of the current line after a newline
  2022-10-24 17:58 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-10-25 10:23   ` Anders Munch
@ 2022-10-26 21:55   ` Jean Abou Samra
  2022-10-27  5:44     ` Eli Zaretskii
  2022-10-27 16:46     ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 2 replies; 14+ messages in thread
From: Jean Abou Samra @ 2022-10-26 21:55 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

Le 24/10/2022 à 19:58, Stefan Monnier via Users list for the GNU Emacs 
text editor a écrit :
>> line when I press RET. What I'd like is for Emacs to auto-insert the
>> number of spaces it deems necessary for the new, still blank line
>> whenever I press RET, without also reindenting the previous line,
> Set `electric-indent-inhibit` to t.


Thanks! This works in Lisp and Scheme. It does not prevent the
C++ mode from reindenting when inserting a semicolon, but I can
live with that one.

Best,
Jean




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

* Re: Disabling automatic reindentation of the current line after a newline
  2022-10-26 21:55   ` Jean Abou Samra
@ 2022-10-27  5:44     ` Eli Zaretskii
  2022-10-27 16:46     ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 14+ messages in thread
From: Eli Zaretskii @ 2022-10-27  5:44 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Wed, 26 Oct 2022 23:55:30 +0200
> Cc: help-gnu-emacs@gnu.org
> From: Jean Abou Samra <jean@abou-samra.fr>
> 
> Le 24/10/2022 à 19:58, Stefan Monnier via Users list for the GNU Emacs 
> text editor a écrit :
> >> line when I press RET. What I'd like is for Emacs to auto-insert the
> >> number of spaces it deems necessary for the new, still blank line
> >> whenever I press RET, without also reindenting the previous line,
> > Set `electric-indent-inhibit` to t.
> 
> 
> Thanks! This works in Lisp and Scheme. It does not prevent the
> C++ mode from reindenting when inserting a semicolon, but I can
> live with that one.

I think in CC Mode, there's another electric variable which controls
the behavior you describe.



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

* Re: Disabling automatic reindentation of the current line after a newline
  2022-10-26 21:55   ` Jean Abou Samra
  2022-10-27  5:44     ` Eli Zaretskii
@ 2022-10-27 16:46     ` Stefan Monnier via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 14+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-10-27 16:46 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Abou Samra [2022-10-26 23:55:30] wrote:
> Le 24/10/2022 à 19:58, Stefan Monnier via Users list for the GNU Emacs text
> editor a écrit :
>>> line when I press RET. What I'd like is for Emacs to auto-insert the
>>> number of spaces it deems necessary for the new, still blank line
>>> whenever I press RET, without also reindenting the previous line,
>> Set `electric-indent-inhibit` to t.
> Thanks! This works in Lisp and Scheme. It does not prevent the
> C++ mode from reindenting when inserting a semicolon, but I can
> live with that one.

You might report this as a bug.  It probably affects all CC-modes :-(


        Stefan




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

end of thread, other threads:[~2022-10-27 16:46 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-22 15:18 Disabling automatic reindentation of the current line after a newline Jean Abou Samra
2022-10-24  2:44 ` Emanuel Berg
2022-10-24 15:29   ` Jean Abou Samra
2022-10-24 21:12     ` Emanuel Berg
  -- strict thread matches above, loose matches on Subject: below --
2022-10-24  9:16 Jean Abou Samra
2022-10-24 17:58 ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-10-25 10:23   ` Anders Munch
2022-10-25 13:03     ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-10-25 13:39       ` Anders Munch
2022-10-26  2:59         ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-10-26  6:04           ` Emanuel Berg
2022-10-26 21:55   ` Jean Abou Samra
2022-10-27  5:44     ` Eli Zaretskii
2022-10-27 16:46     ` Stefan Monnier via Users list for the GNU Emacs text editor

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