From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#14008: Better autoindent for C++11 code? Date: Sun, 13 Oct 2019 18:09:49 +0000 Message-ID: <20191013180949.GE10125@ACM> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="198769"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mutt/1.10.1 (2018-07-13) Cc: 14008@debbugs.gnu.org To: Stefan Kangas Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Oct 13 20:10:12 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iJiJo-000pc4-4D for geb-bug-gnu-emacs@m.gmane.org; Sun, 13 Oct 2019 20:10:12 +0200 Original-Received: from localhost ([::1]:41292 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iJiJm-0000OR-Mk for geb-bug-gnu-emacs@m.gmane.org; Sun, 13 Oct 2019 14:10:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:53071) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iJiJf-0000OK-Nq for bug-gnu-emacs@gnu.org; Sun, 13 Oct 2019 14:10:04 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iJiJe-0001QQ-EW for bug-gnu-emacs@gnu.org; Sun, 13 Oct 2019 14:10:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:56301) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iJiJe-0001QC-Al; Sun, 13 Oct 2019 14:10:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1iJiJe-0008UQ-3a; Sun, 13 Oct 2019 14:10:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Alan Mackenzie Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org, bug-cc-mode@gnu.org Resent-Date: Sun, 13 Oct 2019 18:10:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 14008 X-GNU-PR-Package: emacs,cc-mode Original-Received: via spool by 14008-submit@debbugs.gnu.org id=B14008.157099019732619 (code B ref 14008); Sun, 13 Oct 2019 18:10:02 +0000 Original-Received: (at 14008) by debbugs.gnu.org; 13 Oct 2019 18:09:57 +0000 Original-Received: from localhost ([127.0.0.1]:36889 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1iJiJZ-0008U3-1U for submit@debbugs.gnu.org; Sun, 13 Oct 2019 14:09:57 -0400 Original-Received: from colin.muc.de ([193.149.48.1]:54531 helo=mail.muc.de) by debbugs.gnu.org with smtp (Exim 4.84_2) (envelope-from ) id 1iJiJW-0008Ts-Gb for 14008@debbugs.gnu.org; Sun, 13 Oct 2019 14:09:55 -0400 Original-Received: (qmail 91338 invoked by uid 3782); 13 Oct 2019 18:09:52 -0000 Original-Received: from acm.muc.de (p2E5D5B34.dip0.t-ipconnect.de [46.93.91.52]) by colin.muc.de (tmda-ofmipd) with ESMTP; Sun, 13 Oct 2019 20:09:50 +0200 Original-Received: (qmail 12802 invoked by uid 1000); 13 Oct 2019 18:09:50 -0000 Content-Disposition: inline In-Reply-To: X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:169130 Archived-At: Hello, Stefan and Andrew. On Sun, Oct 13, 2019 at 10:03:00 +0200, Stefan Kangas wrote: > Andrew Pennebaker 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).