From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#24431: 25.1.50; Wrong indentation in C++ mode when calling constructors with braces Date: Sun, 1 Jan 2017 14:32:29 +0000 Message-ID: <20170101143228.GB8392@acm.fritz.box> References: <20160915084234.16623.qmail@mail.muc.de> <1473941364.1627.6.camel@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1483281199 31204 195.159.176.226 (1 Jan 2017 14:33:19 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 1 Jan 2017 14:33:19 +0000 (UTC) User-Agent: Mutt/1.5.24 (2015-08-30) Cc: 24431@debbugs.gnu.org To: Matthias Meulien Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Jan 01 15:33:15 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cNhCA-0006vU-4h for geb-bug-gnu-emacs@m.gmane.org; Sun, 01 Jan 2017 15:33:10 +0100 Original-Received: from localhost ([::1]:53576 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cNhCE-0000XC-4J for geb-bug-gnu-emacs@m.gmane.org; Sun, 01 Jan 2017 09:33:14 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54122) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cNhC7-0000X7-4p for bug-gnu-emacs@gnu.org; Sun, 01 Jan 2017 09:33:08 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cNhC5-0000uQ-NN for bug-gnu-emacs@gnu.org; Sun, 01 Jan 2017 09:33:07 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:46607) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cNhC1-0000tb-Vc; Sun, 01 Jan 2017 09:33:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cNhC1-0003kn-NC; Sun, 01 Jan 2017 09:33:01 -0500 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, 01 Jan 2017 14:33:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 24431 X-GNU-PR-Package: emacs,cc-mode X-GNU-PR-Keywords: Original-Received: via spool by 24431-submit@debbugs.gnu.org id=B24431.148328117614419 (code B ref 24431); Sun, 01 Jan 2017 14:33:01 +0000 Original-Received: (at 24431) by debbugs.gnu.org; 1 Jan 2017 14:32:56 +0000 Original-Received: from localhost ([127.0.0.1]:33773 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cNhBw-0003kU-5e for submit@debbugs.gnu.org; Sun, 01 Jan 2017 09:32:56 -0500 Original-Received: from ocolin.muc.de ([193.149.48.4]:37778 helo=mail.muc.de) by debbugs.gnu.org with smtp (Exim 4.84_2) (envelope-from ) id 1cNhBu-0003kM-1I for 24431@debbugs.gnu.org; Sun, 01 Jan 2017 09:32:54 -0500 Original-Received: (qmail 73600 invoked by uid 3782); 1 Jan 2017 14:32:51 -0000 Original-Received: from acm.muc.de (p4FC469E3.dip0.t-ipconnect.de [79.196.105.227]) by colin.muc.de (tmda-ofmipd) with ESMTP; Sun, 01 Jan 2017 15:32:50 +0100 Original-Received: (qmail 10661 invoked by uid 1000); 1 Jan 2017 14:32:29 -0000 Content-Disposition: inline In-Reply-To: <1473941364.1627.6.camel@gmail.com> 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: 208.118.235.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:127657 Archived-At: Hello, Matthias. Happy New Year! On Thu, Sep 15, 2016 at 02:09:24PM +0200, Matthias Meulien wrote: > Hi Alan, > Sorry, my report lacks of precision. > > > set_line(line_t(point_t(0.4, 0.2), > > >                 point_t(0.2, 0.5))); > > > set_line(line_t{point_t{0.4, 0.2}, > > >       point_t{0.2, 0.5}}); // wrong identation > > > What, exactly, is wrong about that indentation?  What would you like > to see there, and why? > I am expecting to see the same indentation whatever I'm using, brackets > or parenthesis; That is: > set_line(line_t{point_t{0.4, 0.2}, >                 point_t{0.2, 0.5}}); This bug proved quite challenging to fix, and I'm still not finished with it. The syntactic analysis of the construct, at the deepest level of CC Mode, was faulty, and I had to add a new facility to the indentation engine for "an absolute column which won't be amended by further relative offsets" (but don't worry about that). With the patch below, the troublesome line gets indented how we would both want it. Please note that at the moment, this only works in the CC Mode style "gnu", but you've got that set in your file local variables anyway. After applying the patch and recompiling CC Mode, if you are in a running Emacs, please do "M-: (makunbound 'c-style-alist)" and then M-x load-file .../lisp/progmodes/cc-styles.elc . This will cause the variable c-style-alist to be updated to its new value, which simple loading of the file wouldn't achieve. What doesn't work so well is that if that construct is expanded onto several lines (with each brace on its own line) it indents like this: set_line(line_t { point_t { 0.4, 0.2 }, point_t { 0.2, 0.5 } } ); , and also the syntactic analysis shows 'statement-block-intro where it should really be 'brace-list-intro, and so on. I'll be working to fix this. Anyhow, here's the patch. Please try it out, and let me know how well (or how badly) it works. As I said, I'm still working to fix some things. diff -r ea743536b883 cc-align.el --- a/cc-align.el Fri Dec 30 15:19:21 2016 +0000 +++ b/cc-align.el Sun Jan 01 14:17:56 2017 +0000 @@ -278,7 +278,7 @@ (beginning-of-line) (backward-up-list 1) (skip-chars-forward " \t" (c-point 'eol)) - (vector (1+ (current-column))))) + (vector (list (1+ (current-column)))))) (defun c-lineup-arglist-close-under-paren (langelem) "Line up a line under the enclosing open paren. diff -r ea743536b883 cc-engine.el --- a/cc-engine.el Fri Dec 30 15:19:21 2016 +0000 +++ b/cc-engine.el Sun Jan 01 14:17:56 2017 +0000 @@ -10382,6 +10382,30 @@ (c-at-statement-start-p)) (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p nil) +(defun c-looking-at-statement-block () + ;; Point is at an opening brace. If this is a statement block (i.e. the + ;; elements in it are terminated by semicolons) return t. Otherwise, return + ;; nil. + (let ((here (point))) + (prog1 + (if (c-go-list-forward) + (let ((there (point))) + (backward-char) + (c-syntactic-skip-backward + "^;," here t) + (cond + ((eq (char-before) ?\;) t) + ((eq (char-before) ?,) nil) + (t (goto-char here) + (forward-char) + (and (c-syntactic-re-search-forward "{" there t t) + (progn (backward-char) + (c-looking-at-statement-block)))))) + (forward-char) + (and (c-syntactic-re-search-forward "[;,]" nil t t) + (eq (char-before) ?\;))) + (goto-char here)))) + (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end) ;; Return non-nil if we're looking at the beginning of a block ;; inside an expression. The value returned is actually a cons of @@ -10537,15 +10561,7 @@ (and (c-major-mode-is 'c++-mode) (save-excursion (goto-char block-follows) - (if (c-go-list-forward) - (progn - (backward-char) - (c-syntactic-skip-backward - "^;," block-follows t) - (not (eq (char-before) ?\;))) - (or (not (c-syntactic-re-search-forward - "[;,]" nil t t)) - (not (eq (char-before) ?\;))))))) + (not (c-looking-at-statement-block))))) nil (cons 'inexpr-statement (point))))) @@ -10871,6 +10887,14 @@ (cdr (assoc (match-string 1) c-other-decl-block-key-in-symbols-alist)) (max (c-point 'boi paren-pos) (point)))) + ((save-excursion + (goto-char paren-pos) + (c-looking-at-or-maybe-in-bracelist containing-sexp)) + (if (save-excursion + (goto-char paren-pos) + (c-looking-at-statement-block)) + (c-add-syntax 'defun-block-intro nil) + (c-add-syntax 'brace-list-intro nil))) (t (c-add-syntax 'defun-block-intro nil)))) (c-add-syntax 'statement-block-intro nil))) @@ -12692,7 +12716,10 @@ nil)))) (if (or (null res) (integerp res) - (and (vectorp res) (= (length res) 1) (integerp (aref res 0)))) + (and (vectorp res) (= (length res) 1) + (or (integerp (aref res 0)) + (and (consp (aref res 0)) + (integerp (car (aref res 0))))))) res (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S" offset symbol res) @@ -12750,25 +12777,34 @@ (while langelems (let* ((c-syntactic-element (car langelems)) (res (c-calc-offset c-syntactic-element))) - - (if (vectorp res) - ;; Got an absolute column that overrides any indentation - ;; we've collected so far, but not the relative - ;; indentation we might get for the nested structures - ;; further down the langelems list. - (setq indent (elt res 0) - anchor (point-min)) ; A position at column 0. - + (cond + ((and (vectorp res) (consp (elt res 0))) + ;; Got an absolute column that overrides any indentation we've + ;; collected so far. This can only be superseded by another + ;; absolute column (another vector). + (setq indent res + anchor (point-min))) ; A position at column 0. + ((vectorp res) + ;; Got an absolute column that overrides any indentation + ;; we've collected so far, but not the relative + ;; indentation we might get for the nested structures + ;; further down the langelems list. + (setq indent (elt res 0) + anchor (point-min))) ; A position at column 0. + (t ;; Got a relative change of the current calculated ;; indentation. - (setq indent (+ indent res)) + (if (not (vectorp indent)) + (setq indent (+ indent res))) ;; Use the anchor position from the first syntactic ;; element with one. (unless anchor - (setq anchor (c-langelem-pos (car langelems))))) + (setq anchor (c-langelem-pos (car langelems)))))) (setq langelems (cdr langelems)))) + (if (vectorp indent) + (setq indent (car (elt indent 0)))) (if anchor (+ indent (save-excursion diff -r ea743536b883 cc-styles.el --- a/cc-styles.el Fri Dec 30 15:19:21 2016 +0000 +++ b/cc-styles.el Sun Jan 01 14:17:56 2017 +0000 @@ -72,6 +72,7 @@ (arglist-close . c-lineup-arglist) (inline-open . 0) (brace-list-open . +) + (brace-list-intro . c-lineup-arglist-intro-after-paren) (topmost-intro-cont . (first c-lineup-topmost-intro-cont c-lineup-gnu-DEFUN-intro-cont)))) > --  > Matthias -- Alan Mackenzie (Nuremberg, Germany).