unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#14008: Better autoindent for C++11 code?
@ 2013-03-20 15:38 Andrew Pennebaker
  2019-10-13  8:03 ` Stefan Kangas
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Pennebaker @ 2013-03-20 15:38 UTC (permalink / raw)
  To: 14008

[-- Attachment #1: Type: text/plain, Size: 775 bytes --]

C++11 introduced lambda syntax []() { ... } and other strange things that
Emacs indents strangely.

for_each(range.begin(), range.end(), [=](int i) {
    cout << strings[i] << endl;
  });

I would like the final line `});` to have the same indentation level as the
first line `for_each...`.

Here's another example:

for_each(range.begin(), range.end(), [&](int i) {
             std::async(
                        launch::async,
                        [&]() { strings[i] = fizzy(i); }
                        );
  });

The arguments to std::async and its closing parenthesis are indented much
too far; I would like them indented only one level further than where
std::async is itself indented. Any tips for achieving this?

-- 
Cheers,

Andrew Pennebaker
www.yellosoft.us

[-- Attachment #2: Type: text/html, Size: 1536 bytes --]

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

* bug#14008: Better autoindent for C++11 code?
  2013-03-20 15:38 bug#14008: Better autoindent for C++11 code? Andrew Pennebaker
@ 2019-10-13  8:03 ` Stefan Kangas
  2019-10-13 18:09   ` Alan Mackenzie
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Kangas @ 2019-10-13  8:03 UTC (permalink / raw)
  To: Andrew Pennebaker; +Cc: Alan Mackenzie, 14008

Andrew Pennebaker <andrew.pennebaker@gmail.com> writes:

> C++11 introduced lambda syntax []() { ... } and other strange things that Emacs indents strangely.
>
> for_each(range.begin(), range.end(), [=](int i) {
>     cout << strings[i] << endl;
>   });
>
> I would like the final line `});` to have the same indentation level as the first line `for_each...`.

I'm seeing the same thing here.

With "emacs -Q" on current master, this indents like:

for_each(range.begin(), range.end(), [=](int i) {
  cout << strings[i] << endl;
 });

However, note that I have only one space before the final "}"
character, whereas the reporter had two.  I'm not sure if this
behaviour is intentional or not, or if it could be configured.

> Here's another example:
>
> for_each(range.begin(), range.end(), [&](int i) {
>              std::async(
>                         launch::async,
>                         [&]() { strings[i] = fizzy(i); }
>                         );
>   });
>
> The arguments to std::async and its closing parenthesis are indented much too far; I would like them indented only one level further than where std::async is itself indented. Any tips for achieving this?

I'm seeing something similar here:

for_each(range.begin(), range.end(), [&](int i) {
  std::async(
         launch::async,
         [&]() { strings[i] = fizzy(i); }
         );
 });

Perhaps Alan could clarify if this is a bug or if this is just a case
of missing configuration?

Best regards,
Stefan Kangas





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

* bug#14008: Better autoindent for C++11 code?
  2019-10-13  8:03 ` Stefan Kangas
@ 2019-10-13 18:09   ` Alan Mackenzie
  2019-11-03 13:29     ` Stefan Kangas
  0 siblings, 1 reply; 6+ messages in thread
From: Alan Mackenzie @ 2019-10-13 18:09 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: 14008

Hello, Stefan and Andrew.

On Sun, Oct 13, 2019 at 10:03:00 +0200, Stefan Kangas wrote:
> Andrew Pennebaker <andrew.pennebaker@gmail.com> writes:

> > C++11 introduced lambda syntax []() { ... } and other strange things that Emacs indents strangely.

> > for_each(range.begin(), range.end(), [=](int i) {
> >     cout << strings[i] << endl;
> >   });

> > I would like the final line `});` to have the same indentation level
> > as the first line `for_each...`.

> I'm seeing the same thing here.

I think this is the minimum indentation imposed on all lines within "code
blocks" in "gnu" style.  (See the CC Mode manual for a description of CC
Mode's style system.)  If you indent the for_each line by, say, 4 columns
(put it inside braces if needed) the line with }); then gets indented
under the for_each.  As for_each is a function (pretty much equivalent to
Lisp's mapc), it's unlikely to be put at column 0 in real source code.

> With "emacs -Q" on current master, this indents like:

> for_each(range.begin(), range.end(), [=](int i) {
>   cout << strings[i] << endl;
>  });

> However, note that I have only one space before the final "}"
> character, whereas the reporter had two.  I'm not sure if this
> behaviour is intentional or not, or if it could be configured.

I'm surprised about the two spaces too, I don't understand how it could
have happened, unless there's been some disruption in the copying from
Emacs screen to email.

> > Here's another example:

> > for_each(range.begin(), range.end(), [&](int i) {
> >              std::async(
> >                         launch::async,
> >                         [&]() { strings[i] = fizzy(i); }
> >                         );
> >   });

> > The arguments to std::async and its closing parenthesis are indented
> > much too far; I would like them indented only one level further than
> > where std::async is itself indented. Any tips for achieving this?

Configure CC Mode, either by activating a different style (with C-c ., or
M-x c-set-style if some minor mode is using that key binding), or by
directly setting syntactic symbols' "offsets".

For the first possibility, I'd recommend trying out, say styles "bsd" or
"linux".

For the second, there are several ways to set symbols' offsets (see page
"Config Basics" in the CC Mode manual).  A good way is by using a hook
function.

To find out what syntactic symbols you need to change, put point on a
pertinent line and type C-c C-s.  For example on the "launch::async" line
you'd see something like "((arglist-intro 394 404))", the two numbers
being the "anchor points" from which the indentation is done.  To see
(and change temporarily) the "offset" for a symbol, type C-c C-o.  The
"gnu" style setting of the "offset" for arglist-intro is the function
c-lineup-arglist-intro-after-paren, which does pretty much what its name
says.

I think you want a simple extra level of indentation from point 394, so
you could construct your hook function something like this:

(defun my-c-indent ()
  (c-set-offset 'arglist-intro '+)
  ;; Add any further syntactic symbols here.
  )
(add-hook 'c-mode-common-hook #'my-c-indent) ; c++-mode-hook could also
                                             ; be used

Again, I strongly recommend reading the relevant pages in the CC Mode
manual.


> I'm seeing something similar here:

> for_each(range.begin(), range.end(), [&](int i) {
>   std::async(
>          launch::async,
>          [&]() { strings[i] = fizzy(i); }
>          );
>  });

> Perhaps Alan could clarify if this is a bug or if this is just a case
> of missing configuration?

A bit of a mixture of both, I think.

> Best regards,
> Stefan Kangas

-- 
Alan Mackenzie (Nuremberg, Germany).





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

* bug#14008: Better autoindent for C++11 code?
  2019-10-13 18:09   ` Alan Mackenzie
@ 2019-11-03 13:29     ` Stefan Kangas
  2019-11-07 18:19       ` Alan Mackenzie
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Kangas @ 2019-11-03 13:29 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: 14008

Hi Alan,

Alan Mackenzie <acm@muc.de> writes:

>> > C++11 introduced lambda syntax []() { ... } and other strange things that Emacs indents strangely.
>
>> > for_each(range.begin(), range.end(), [=](int i) {
>> >     cout << strings[i] << endl;
>> >   });
>
>> > I would like the final line `});` to have the same indentation level
>> > as the first line `for_each...`.
>
>> I'm seeing the same thing here.
>
> I think this is the minimum indentation imposed on all lines within "code
> blocks" in "gnu" style.  (See the CC Mode manual for a description of CC
> Mode's style system.)  If you indent the for_each line by, say, 4 columns
> (put it inside braces if needed) the line with }); then gets indented
> under the for_each.  As for_each is a function (pretty much equivalent to
> Lisp's mapc), it's unlikely to be put at column 0 in real source code.

Indeed.  It seems to work better when you put it in a function.

>> > Here's another example:
>
>> > for_each(range.begin(), range.end(), [&](int i) {
>> >              std::async(
>> >                         launch::async,
>> >                         [&]() { strings[i] = fizzy(i); }
>> >                         );
>> >   });
>
>> > The arguments to std::async and its closing parenthesis are indented
>> > much too far; I would like them indented only one level further than
>> > where std::async is itself indented. Any tips for achieving this?
>
> Configure CC Mode, either by activating a different style (with C-c ., or
> M-x c-set-style if some minor mode is using that key binding), or by
> directly setting syntactic symbols' "offsets".
[...]
> Again, I strongly recommend reading the relevant pages in the CC Mode
> manual.
[...]
>> Perhaps Alan could clarify if this is a bug or if this is just a case
>> of missing configuration?
>
> A bit of a mixture of both, I think.

Do you think there is anything more to do here, or should this bug be
closed?

Best regards,
Stefan Kangas





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

* bug#14008: Better autoindent for C++11 code?
  2019-11-03 13:29     ` Stefan Kangas
@ 2019-11-07 18:19       ` Alan Mackenzie
  2019-11-07 23:41         ` Stefan Kangas
  0 siblings, 1 reply; 6+ messages in thread
From: Alan Mackenzie @ 2019-11-07 18:19 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: 14008

Hello, Stefan.

On Sun, Nov 03, 2019 at 14:29:13 +0100, Stefan Kangas wrote:
> Hi Alan,

> Alan Mackenzie <acm@muc.de> writes:

> >> > C++11 introduced lambda syntax []() { ... } and other strange things that Emacs indents strangely.

> >> > for_each(range.begin(), range.end(), [=](int i) {
> >> >     cout << strings[i] << endl;
> >> >   });

> >> > I would like the final line `});` to have the same indentation level
> >> > as the first line `for_each...`.

> >> I'm seeing the same thing here.

> > I think this is the minimum indentation imposed on all lines within "code
> > blocks" in "gnu" style.  (See the CC Mode manual for a description of CC
> > Mode's style system.)  If you indent the for_each line by, say, 4 columns
> > (put it inside braces if needed) the line with }); then gets indented
> > under the for_each.  As for_each is a function (pretty much equivalent to
> > Lisp's mapc), it's unlikely to be put at column 0 in real source code.

> Indeed.  It seems to work better when you put it in a function.

> >> > Here's another example:

> >> > for_each(range.begin(), range.end(), [&](int i) {
> >> >              std::async(
> >> >                         launch::async,
> >> >                         [&]() { strings[i] = fizzy(i); }
> >> >                         );
> >> >   });

> >> > The arguments to std::async and its closing parenthesis are indented
> >> > much too far; I would like them indented only one level further than
> >> > where std::async is itself indented. Any tips for achieving this?

> > Configure CC Mode, either by activating a different style (with C-c ., or
> > M-x c-set-style if some minor mode is using that key binding), or by
> > directly setting syntactic symbols' "offsets".
> [...]
> > Again, I strongly recommend reading the relevant pages in the CC Mode
> > manual.
> [...]
> >> Perhaps Alan could clarify if this is a bug or if this is just a case
> >> of missing configuration?

> > A bit of a mixture of both, I think.

> Do you think there is anything more to do here, or should this bug be
> closed?

I think the bug should now be closed.

> Best regards,
> Stefan Kangas

-- 
Alan Mackenzie (Nuremberg, Germany).





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

* bug#14008: Better autoindent for C++11 code?
  2019-11-07 18:19       ` Alan Mackenzie
@ 2019-11-07 23:41         ` Stefan Kangas
  0 siblings, 0 replies; 6+ messages in thread
From: Stefan Kangas @ 2019-11-07 23:41 UTC (permalink / raw)
  To: Alan Mackenzie; +Cc: 14008-done

Alan Mackenzie <acm@muc.de> writes:

>> Do you think there is anything more to do here, or should this bug be
>> closed?
>
> I think the bug should now be closed.

Thanks, I'm therefore closing this bug now.

Best regards,
Stefan Kangas





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

end of thread, other threads:[~2019-11-07 23:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-20 15:38 bug#14008: Better autoindent for C++11 code? Andrew Pennebaker
2019-10-13  8:03 ` Stefan Kangas
2019-10-13 18:09   ` Alan Mackenzie
2019-11-03 13:29     ` Stefan Kangas
2019-11-07 18:19       ` Alan Mackenzie
2019-11-07 23:41         ` Stefan Kangas

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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