aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Mackenzie2017-02-01 20:20:09 +0000
committerAlan Mackenzie2017-02-01 20:20:09 +0000
commit94ad13b93c6fc099a353c8eb27c00a68ee79a952 (patch)
tree4e68066d5b6431089ab1c44092c46995d26c42ba
parent5d61ef0de9a3f5b1be9e93465cb88aae995975ba (diff)
downloademacs-94ad13b93c6fc099a353c8eb27c00a68ee79a952.tar.gz
emacs-94ad13b93c6fc099a353c8eb27c00a68ee79a952.zip
Allow C++ nested brace-list-entries to be better indented.
This fixes bug #24431. The key change of this bug fix is correctly analyzing nested brace lists when the opening element stands on the same line as both its introductory brace and an enclosing parameter list parenthesis. * list/progmodes/cc-align.el (c-lineup-under-anchor): New line-up function. * list/progmodes/cc-engine.el (c-looking-at-or-maybe-in-bracelist): Accept the presence of exactly an identifier between an open parenthesis and an open brace as evidence of the brace starting a brace list. (c-looking-at-statement-block): New function, extracted from c-looking-at-inexpr-block. Enhance it to analyze inner blocks recursively when needed. (c-looking-at-inexpr-block): Extract new function (see above) and call it. (c-add-stmt-syntax): Enhance, with new &optional parameter, to supply the prime syntactic symbol with a fixed anchor point. When this is used, restrict all added syntactic symbols to those having an anchor point on the same line. Add, in addition to the current additional symbols, c-brace-list-entry when needed; use c-looking-at-statement-block to determine the latter. (c-guess-basic-syntax, CASE 9D): Use c-add-stmt-syntax rather than just c-add-syntax, to assemble the syntactic context of a 'brace-list-entry, thus getting, possibly, several accompanying syntactic entries. * lisp/progmodes/cc-styles.el (c-style-alist, "gnu" style): New entry for 'brace-list-intro, namely c-lineup-arglist-intro-after-paren. * lisp/progmodes/cc-vars.el (c-offsets-alist): Change the factory default offset for 'brace-list-entry from 0 to c-lineup-under-anchor. * doc/misc/cc-mode.texi (Syntactic Symbols): Amend the definition of brace-list-intro. (Brace List Symbols): Amend the example to show the new analysis of brace lists when the first element comes on the same line as the opening brace. (Misc Line-Up): Document the new line-up function c-lineup-under-anchor.
-rw-r--r--doc/misc/cc-mode.texi31
-rw-r--r--lisp/progmodes/cc-align.el12
-rw-r--r--lisp/progmodes/cc-engine.el101
-rw-r--r--lisp/progmodes/cc-styles.el1
-rw-r--r--lisp/progmodes/cc-vars.el2
5 files changed, 111 insertions, 36 deletions
diff --git a/doc/misc/cc-mode.texi b/doc/misc/cc-mode.texi
index 68a16c0ed74..14981c9c58b 100644
--- a/doc/misc/cc-mode.texi
+++ b/doc/misc/cc-mode.texi
@@ -4141,7 +4141,8 @@ Open brace of an enum or static array list. @ref{Brace List Symbols}.
4141@item brace-list-close 4141@item brace-list-close
4142Close brace of an enum or static array list. @ref{Brace List Symbols}. 4142Close brace of an enum or static array list. @ref{Brace List Symbols}.
4143@item brace-list-intro 4143@item brace-list-intro
4144First line in an enum or static array list. @ref{Brace List Symbols}. 4144First line after the opening @samp{@{} in an enum or static array
4145list. @ref{Brace List Symbols}.
4145@item brace-list-entry 4146@item brace-list-entry
4146Subsequent lines in an enum or static array list. @ref{Brace List 4147Subsequent lines in an enum or static array list. @ref{Brace List
4147Symbols}. 4148Symbols}.
@@ -4635,11 +4636,18 @@ example:
4635 4636
4636Here, you've already seen the analysis of lines 1, 2, 3, and 11. On 4637Here, you've already seen the analysis of lines 1, 2, 3, and 11. On
4637line 4, things get interesting; this line is assigned 4638line 4, things get interesting; this line is assigned
4638@code{brace-entry-open} syntactic symbol because it's a bracelist entry 4639@code{brace-entry-open} syntactic symbol because it's a bracelist
4639line that starts with an open brace. Lines 5 and 6 (and line 9) are 4640entry line that starts with an open brace. Lines 5 and 6 are pretty
4640pretty standard, and line 7 is a @code{brace-list-close} as you'd 4641standard, and line 7 is a @code{brace-list-close} as you'd expect.
4641expect. Once again, line 8 is assigned as @code{brace-entry-open} as is 4642Once again, line 8 is assigned as @code{brace-entry-open} as is line
4642line 10. 464310. Line 9 is assigned two syntactic elements, @code{brace-list-intro}
4644with anchor point at the @samp{@{} of line 8@footnote{This extra
4645syntactic element was introduced in @ccmode{} 5.33.1 to allow extra
4646flexibility in indenting the second line of such a construct. You can
4647preserve the behaviour resulting from the former syntactic analysis by
4648giving @code{brace-list-entry} an offset of
4649@code{c-lineup-under-anchor} (@pxref{Misc Line-Up}).}, and
4650@code{brace-list-entry} anchored on the @samp{1} of line 8.
4643 4651
4644@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 4652@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4645@node External Scope Symbols, Paren List Symbols, Brace List Symbols, Syntactic Symbols 4653@node External Scope Symbols, Paren List Symbols, Brace List Symbols, Syntactic Symbols
@@ -6288,6 +6296,17 @@ already has; think of it as an identity function for lineups.
6288 6296
6289@comment ------------------------------------------------------------ 6297@comment ------------------------------------------------------------
6290 6298
6299@defun c-lineup-under-anchor
6300
6301Line up a line directly underneath its anchor point. This is like
6302@samp{0}, except any previously calculated offset contributions are
6303disregarded.
6304
6305@workswith Any syntactic symbol which has an anchor point.
6306@end defun
6307
6308@comment ------------------------------------------------------------
6309
6291@defun c-lineup-cpp-define 6310@defun c-lineup-cpp-define
6292@findex lineup-cpp-define (c-) 6311@findex lineup-cpp-define (c-)
6293Line up macro continuation lines according to the indentation of the 6312Line up macro continuation lines according to the indentation of the
diff --git a/lisp/progmodes/cc-align.el b/lisp/progmodes/cc-align.el
index 7cb36c4396b..0f7e4b598dc 100644
--- a/lisp/progmodes/cc-align.el
+++ b/lisp/progmodes/cc-align.el
@@ -1221,6 +1221,18 @@ Works with: arglist-cont, arglist-cont-nonempty."
1221 1221
1222 (vector (progn (goto-char alignto) (current-column))))))) 1222 (vector (progn (goto-char alignto) (current-column)))))))
1223 1223
1224(defun c-lineup-under-anchor (langelem)
1225 "Line up the current line directly under the anchor position in LANGELEM.
1226
1227This is like 0, except it supersedes any indentation already calculated for
1228previous syntactic elements in the syntactic context.
1229
1230Works with: Any syntactic symbol which has an anchor position."
1231 (save-excursion
1232 (goto-char (c-langelem-pos langelem))
1233 (vector (current-column))))
1234
1235
1224(defun c-lineup-dont-change (langelem) 1236(defun c-lineup-dont-change (langelem)
1225 "Do not change the indentation of the current line. 1237 "Do not change the indentation of the current line.
1226 1238
diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index fd7aa50840f..dfd7aebd569 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -10260,13 +10260,22 @@ comment at the start of cc-engine.el for more info."
10260 (t nil))))) 10260 (t nil)))))
10261 10261
10262 (setq pos (point)) 10262 (setq pos (point))
10263 (if (and after-type-id-pos 10263 (cond
10264 (goto-char after-type-id-pos) 10264 ((and after-type-id-pos
10265 (setq res (c-back-over-member-initializers)) 10265 (goto-char after-type-id-pos)
10266 (goto-char res) 10266 (setq res (c-back-over-member-initializers))
10267 (eq (car (c-beginning-of-decl-1 lim)) 'same)) 10267 (goto-char res)
10268 (cons (point) nil) ; Return value. 10268 (eq (car (c-beginning-of-decl-1 lim)) 'same))
10269 10269 (cons (point) nil)) ; Return value.
10270
10271 ((and after-type-id-pos
10272 (progn
10273 (c-backward-syntactic-ws)
10274 (eq (char-before) ?\()))
10275 ;; Single identifier between '(' and '{'. We have a bracelist.
10276 (cons after-type-id-pos nil))
10277
10278 (t
10270 (goto-char pos) 10279 (goto-char pos)
10271 ;; Checks to do on all sexps before the brace, up to the 10280 ;; Checks to do on all sexps before the brace, up to the
10272 ;; beginning of the statement. 10281 ;; beginning of the statement.
@@ -10368,7 +10377,7 @@ comment at the start of cc-engine.el for more info."
10368 ; languages where 10377 ; languages where
10369 ; `c-opt-inexpr-brace-list-key' is 10378 ; `c-opt-inexpr-brace-list-key' is
10370 ; non-nil and we have macros. 10379 ; non-nil and we have macros.
10371 (t t))) ;; The caller can go up one level. 10380 (t t)))) ;; The caller can go up one level.
10372 ))) 10381 )))
10373 10382
10374(defun c-inside-bracelist-p (containing-sexp paren-state) 10383(defun c-inside-bracelist-p (containing-sexp paren-state)
@@ -10493,6 +10502,30 @@ comment at the start of cc-engine.el for more info."
10493 (c-at-statement-start-p)) 10502 (c-at-statement-start-p))
10494(make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1") 10503(make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
10495 10504
10505(defun c-looking-at-statement-block ()
10506 ;; Point is at an opening brace. If this is a statement block (i.e. the
10507 ;; elements in it are terminated by semicolons) return t. Otherwise, return
10508 ;; nil.
10509 (let ((here (point)))
10510 (prog1
10511 (if (c-go-list-forward)
10512 (let ((there (point)))
10513 (backward-char)
10514 (c-syntactic-skip-backward
10515 "^;," here t)
10516 (cond
10517 ((eq (char-before) ?\;) t)
10518 ((eq (char-before) ?,) nil)
10519 (t (goto-char here)
10520 (forward-char)
10521 (and (c-syntactic-re-search-forward "{" there t t)
10522 (progn (backward-char)
10523 (c-looking-at-statement-block))))))
10524 (forward-char)
10525 (and (c-syntactic-re-search-forward "[;,]" nil t t)
10526 (eq (char-before) ?\;)))
10527 (goto-char here))))
10528
10496(defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end) 10529(defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
10497 ;; Return non-nil if we're looking at the beginning of a block 10530 ;; Return non-nil if we're looking at the beginning of a block
10498 ;; inside an expression. The value returned is actually a cons of 10531 ;; inside an expression. The value returned is actually a cons of
@@ -10648,15 +10681,7 @@ comment at the start of cc-engine.el for more info."
10648 (and (c-major-mode-is 'c++-mode) 10681 (and (c-major-mode-is 'c++-mode)
10649 (save-excursion 10682 (save-excursion
10650 (goto-char block-follows) 10683 (goto-char block-follows)
10651 (if (c-go-list-forward) 10684 (not (c-looking-at-statement-block)))))
10652 (progn
10653 (backward-char)
10654 (c-syntactic-skip-backward
10655 "^;," block-follows t)
10656 (not (eq (char-before) ?\;)))
10657 (or (not (c-syntactic-re-search-forward
10658 "[;,]" nil t t))
10659 (not (eq (char-before) ?\;)))))))
10660 nil 10685 nil
10661 (cons 'inexpr-statement (point))))) 10686 (cons 'inexpr-statement (point)))))
10662 10687
@@ -10792,17 +10817,20 @@ comment at the start of cc-engine.el for more info."
10792 syntax-extra-args 10817 syntax-extra-args
10793 stop-at-boi-only 10818 stop-at-boi-only
10794 containing-sexp 10819 containing-sexp
10795 paren-state) 10820 paren-state
10821 &optional fixed-anchor)
10796 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as 10822 ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
10797 ;; needed with further syntax elements of the types `substatement', 10823 ;; needed with further syntax elements of the types `substatement',
10798 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and 10824 ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro',
10799 ;; `defun-block-intro'. 10825 ;; `defun-block-intro', and `brace-list-intro'.
10800 ;; 10826 ;;
10801 ;; Do the generic processing to anchor the given syntax symbol on 10827 ;; Do the generic processing to anchor the given syntax symbol on the
10802 ;; the preceding statement: Skip over any labels and containing 10828 ;; preceding statement: First skip over any labels and containing statements
10803 ;; statements on the same line, and then search backward until we 10829 ;; on the same line. If FIXED-ANCHOR is non-nil, use this as the
10804 ;; find a statement or block start that begins at boi without a 10830 ;; anchor-point for the given syntactic symbol, and don't make syntactic
10805 ;; label or comment. 10831 ;; entries for constructs beginning on lines before that containing
10832 ;; ANCHOR-POINT. Otherwise search backward until we find a statement or
10833 ;; block start that begins at boi without a label or comment.
10806 ;; 10834 ;;
10807 ;; Point is assumed to be at the prospective anchor point for the 10835 ;; Point is assumed to be at the prospective anchor point for the
10808 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to 10836 ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
@@ -10831,6 +10859,7 @@ comment at the start of cc-engine.el for more info."
10831 10859
10832 (let ((syntax-last c-syntactic-context) 10860 (let ((syntax-last c-syntactic-context)
10833 (boi (c-point 'boi)) 10861 (boi (c-point 'boi))
10862 (anchor-boi (c-point 'boi))
10834 ;; Set when we're on a label, so that we don't stop there. 10863 ;; Set when we're on a label, so that we don't stop there.
10835 ;; FIXME: To be complete we should check if we're on a label 10864 ;; FIXME: To be complete we should check if we're on a label
10836 ;; now at the start. 10865 ;; now at the start.
@@ -10908,7 +10937,9 @@ comment at the start of cc-engine.el for more info."
10908 (c-add-syntax 'substatement nil)))) 10937 (c-add-syntax 'substatement nil))))
10909 ))) 10938 )))
10910 10939
10911 containing-sexp) 10940 containing-sexp
10941 (or (null fixed-anchor)
10942 (> containing-sexp anchor-boi)))
10912 10943
10913 ;; Now we have to go out of this block. 10944 ;; Now we have to go out of this block.
10914 (goto-char containing-sexp) 10945 (goto-char containing-sexp)
@@ -10982,6 +11013,14 @@ comment at the start of cc-engine.el for more info."
10982 (cdr (assoc (match-string 1) 11013 (cdr (assoc (match-string 1)
10983 c-other-decl-block-key-in-symbols-alist)) 11014 c-other-decl-block-key-in-symbols-alist))
10984 (max (c-point 'boi paren-pos) (point)))) 11015 (max (c-point 'boi paren-pos) (point))))
11016 ((save-excursion
11017 (goto-char paren-pos)
11018 (c-looking-at-or-maybe-in-bracelist containing-sexp))
11019 (if (save-excursion
11020 (goto-char paren-pos)
11021 (c-looking-at-statement-block))
11022 (c-add-syntax 'defun-block-intro nil)
11023 (c-add-syntax 'brace-list-intro nil)))
10985 (t (c-add-syntax 'defun-block-intro nil)))) 11024 (t (c-add-syntax 'defun-block-intro nil))))
10986 11025
10987 (c-add-syntax 'statement-block-intro nil))) 11026 (c-add-syntax 'statement-block-intro nil)))
@@ -11001,7 +11040,10 @@ comment at the start of cc-engine.el for more info."
11001 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)] 11040 (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
11002 (while q 11041 (while q
11003 (unless (car q) 11042 (unless (car q)
11004 (setcar q (point))) 11043 (setcar q (if (or (cdr p)
11044 (null fixed-anchor))
11045 (point)
11046 fixed-anchor)))
11005 (setq q (cdr q))) 11047 (setq q (cdr q)))
11006 (setq p (cdr p)))) 11048 (setq p (cdr p))))
11007 ))) 11049 )))
@@ -12354,7 +12396,8 @@ comment at the start of cc-engine.el for more info."
12354 (c-forward-syntactic-ws (c-point 'eol)) 12396 (c-forward-syntactic-ws (c-point 'eol))
12355 (c-looking-at-special-brace-list (point))))) 12397 (c-looking-at-special-brace-list (point)))))
12356 (c-add-syntax 'brace-entry-open (point)) 12398 (c-add-syntax 'brace-entry-open (point))
12357 (c-add-syntax 'brace-list-entry (point)) 12399 (c-add-stmt-syntax 'brace-list-entry nil t containing-sexp
12400 paren-state (point))
12358 )) 12401 ))
12359 )))) 12402 ))))
12360 12403
@@ -12848,7 +12891,7 @@ Cannot combine absolute offsets %S and %S in `add' method"
12848 ;; 12891 ;;
12849 ;; Note that topmost-intro always has an anchor position at bol, for 12892 ;; Note that topmost-intro always has an anchor position at bol, for
12850 ;; historical reasons. It's often used together with other symbols 12893 ;; historical reasons. It's often used together with other symbols
12851 ;; that has more sane positions. Since we always use the first 12894 ;; that have more sane positions. Since we always use the first
12852 ;; found anchor position, we rely on that these other symbols always 12895 ;; found anchor position, we rely on that these other symbols always
12853 ;; precede topmost-intro in the LANGELEMS list. 12896 ;; precede topmost-intro in the LANGELEMS list.
12854 ;; 12897 ;;
diff --git a/lisp/progmodes/cc-styles.el b/lisp/progmodes/cc-styles.el
index d3505490505..b3848a74f97 100644
--- a/lisp/progmodes/cc-styles.el
+++ b/lisp/progmodes/cc-styles.el
@@ -67,6 +67,7 @@
67 (arglist-close . c-lineup-arglist) 67 (arglist-close . c-lineup-arglist)
68 (inline-open . 0) 68 (inline-open . 0)
69 (brace-list-open . +) 69 (brace-list-open . +)
70 (brace-list-intro . c-lineup-arglist-intro-after-paren)
70 (topmost-intro-cont 71 (topmost-intro-cont
71 . (first c-lineup-topmost-intro-cont 72 . (first c-lineup-topmost-intro-cont
72 c-lineup-gnu-DEFUN-intro-cont)))) 73 c-lineup-gnu-DEFUN-intro-cont))))
diff --git a/lisp/progmodes/cc-vars.el b/lisp/progmodes/cc-vars.el
index a6a96d15188..1114b21381d 100644
--- a/lisp/progmodes/cc-vars.el
+++ b/lisp/progmodes/cc-vars.el
@@ -1115,7 +1115,7 @@ can always override the use of `c-default-style' by making calls to
1115 ;; Anchor pos: At the brace list decl start(*). 1115 ;; Anchor pos: At the brace list decl start(*).
1116 (brace-list-intro . +) 1116 (brace-list-intro . +)
1117 ;; Anchor pos: At the brace list decl start(*). 1117 ;; Anchor pos: At the brace list decl start(*).
1118 (brace-list-entry . 0) 1118 (brace-list-entry . c-lineup-under-anchor)
1119 ;; Anchor pos: At the first non-ws char after the open paren if 1119 ;; Anchor pos: At the first non-ws char after the open paren if
1120 ;; the first token is on the same line, otherwise boi at that 1120 ;; the first token is on the same line, otherwise boi at that
1121 ;; token. 1121 ;; token.