1;;; cc-align.el --- custom indentation functions for CC Mode 2 3;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 4;; 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 5;; Free Software Foundation, Inc. 6 7;; Authors: 2004- Alan Mackenzie 8;; 1998- Martin Stjernholm 9;; 1992-1999 Barry A. Warsaw 10;; 1987 Dave Detlefs and Stewart Clamen 11;; 1985 Richard M. Stallman 12;; Maintainer: bug-cc-mode@gnu.org 13;; Created: 22-Apr-1997 (split from cc-mode.el) 14;; Version: See cc-mode.el 15;; Keywords: c languages oop 16 17;; This file is part of GNU Emacs. 18 19;; GNU Emacs is free software; you can redistribute it and/or modify 20;; it under the terms of the GNU General Public License as published by 21;; the Free Software Foundation; either version 2, or (at your option) 22;; any later version. 23 24;; GNU Emacs is distributed in the hope that it will be useful, 25;; but WITHOUT ANY WARRANTY; without even the implied warranty of 26;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 27;; GNU General Public License for more details. 28 29;; You should have received a copy of the GNU General Public License 30;; along with this program; see the file COPYING. If not, write to 31;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 32;; Boston, MA 02110-1301, USA. 33 34;;; Commentary: 35 36;;; Code: 37 38(eval-when-compile 39 (let ((load-path 40 (if (and (boundp 'byte-compile-dest-file) 41 (stringp byte-compile-dest-file)) 42 (cons (file-name-directory byte-compile-dest-file) load-path) 43 load-path))) 44 (load "cc-bytecomp" nil t))) 45 46(cc-require 'cc-defs) 47(cc-require 'cc-vars) 48(cc-require 'cc-engine) 49 50 51;; Standard line-up functions 52;; 53;; See the section "Custom Indentation Functions" in the manual for 54;; details on the calling convention. 55 56(defun c-lineup-topmost-intro-cont (langelem) 57 "Line up declaration continuation lines zero or one indentation step. 58For lines in the \"header\" of a definition, zero is used. For other 59lines, `c-basic-offset' is added to the indentation. E.g: 60 61int 62neg (int i) <- c-lineup-topmost-intro-cont 63{ 64 return -i; 65} 66 67struct 68larch <- c-lineup-topmost-intro-cont 69{ 70 double height; 71} 72 the_larch, <- c-lineup-topmost-intro-cont 73 another_larch; <- c-lineup-topmost-intro-cont 74<--> c-basic-offset 75 76struct larch 77the_larch, <- c-lineup-topmost-intro-cont 78 another_larch; <- c-lineup-topmost-intro-cont 79 80\(This function is mainly provided to mimic the behavior of CC Mode 815.28 and earlier where this case wasn't handled consistently so that 82these lines could be analyzed as either topmost-intro-cont or 83statement-cont.) 84 85Works with: topmost-intro-cont." 86 (save-excursion 87 (beginning-of-line) 88 (c-backward-syntactic-ws (c-langelem-pos langelem)) 89 (if (and (memq (char-before) '(?} ?,)) 90 (not (and c-overloadable-operators-regexp 91 (c-after-special-operator-id)))) 92 c-basic-offset))) 93 94(defun c-lineup-gnu-DEFUN-intro-cont (langelem) 95 "Line up the continuation lines of a DEFUN macro in the Emacs C source. 96These lines are indented as though they were `knr-argdecl-intro' lines. 97Return nil when we're not in such a construct. 98 99This function is for historical compatibility with how previous CC Modes (5.28 100and earlier) indented such lines. 101 102Here is an example: 103 104DEFUN (\"forward-char\", Fforward_char, Sforward_char, 0, 1, \"p\", 105 doc: /* Move point right N characters (left if N is negative). 106On reaching end of buffer, stop and signal error. */) 107 (n) <- c-lineup-gnu-DEFUN-into-cont 108 Lisp_Object n; <- c-lineup-gnu-DEFUN-into-cont 109 110Works with: topmost-intro-cont." 111 (save-excursion 112 (let (case-fold-search) 113 (goto-char (c-langelem-pos langelem)) 114 (if (looking-at "\\<DEFUN\\>") 115 (c-calc-offset '(knr-argdecl-intro)))))) 116 117(defun c-block-in-arglist-dwim (arglist-start) 118 ;; This function implements the DWIM to avoid far indentation of 119 ;; brace block constructs in arguments in `c-lineup-arglist' etc. 120 ;; Return non-nil if a brace block construct is detected within the 121 ;; arglist starting at ARGLIST-START. 122 123 (or 124 ;; Check if the syntactic context contains any of the symbols for 125 ;; in-expression constructs. This can both save the work that we 126 ;; have to do below, and it also detect the brace list constructs 127 ;; that `c-looking-at-inexpr-block' currently misses (they are 128 ;; recognized by `c-inside-bracelist-p' instead). 129 (assq 'inexpr-class c-syntactic-context) 130 (assq 'inexpr-statement c-syntactic-context) 131 (assq 'inlambda c-syntactic-context) 132 133 (save-restriction 134 ;; Search for open braces from the arglist start to the end of the 135 ;; line. 136 (narrow-to-region arglist-start (c-point 'eol arglist-start)) 137 138 (goto-char arglist-start) 139 (while (and (c-syntactic-re-search-forward "{" nil t) 140 (progn 141 (backward-char) 142 (or 143 ;; Ignore starts of special brace lists. 144 (and c-special-brace-lists 145 (save-restriction 146 (widen) 147 (c-looking-at-special-brace-list))) 148 ;; Ignore complete blocks. 149 (c-safe (c-forward-sexp) t)))) 150 (forward-char)) 151 152 (looking-at "{")) 153 154 (let (containing-sexp) 155 (goto-char arglist-start) 156 ;; `c-syntactic-eol' always matches somewhere on the line. 157 (re-search-forward c-syntactic-eol) 158 (goto-char (match-beginning 0)) 159 (c-forward-syntactic-ws) 160 (setq containing-sexp (c-most-enclosing-brace (c-parse-state))) 161 (c-looking-at-inexpr-block 162 (c-safe-position (or containing-sexp (point)) c-state-cache) 163 containing-sexp)))) 164 165(defun c-lineup-arglist (langelem) 166 "Line up the current argument line under the first argument. 167 168As a special case, if the indented line is inside a brace block 169construct, the indentation is `c-basic-offset' only. This is intended 170as a \"DWIM\" measure in cases like macros that contains statement 171blocks, e.g: 172 173A_VERY_LONG_MACRO_NAME ({ 174 some (code, with + long, lines * in[it]); 175 }); 176<--> c-basic-offset 177 178This is motivated partly because it's more in line with how code 179blocks are handled, and partly since it approximates the behavior of 180earlier CC Mode versions, which due to inaccurate analysis tended to 181indent such cases this way. 182 183Works with: arglist-cont-nonempty, arglist-close." 184 (save-excursion 185 (let ((indent-pos (point))) 186 187 (if (c-block-in-arglist-dwim (c-langelem-2nd-pos c-syntactic-element)) 188 c-basic-offset ; DWIM case. 189 190 ;; Normal case. Indent to the token after the arglist open paren. 191 (goto-char (c-langelem-2nd-pos c-syntactic-element)) 192 (if (and c-special-brace-lists 193 (c-looking-at-special-brace-list)) 194 ;; Skip a special brace list opener like "({". 195 (progn (c-forward-token-2) 196 (forward-char)) 197 (forward-char)) 198 (let ((arglist-content-start (point))) 199 (c-forward-syntactic-ws) 200 (when (< (point) indent-pos) 201 (goto-char arglist-content-start) 202 (skip-chars-forward " \t")) 203 (vector (current-column))))))) 204 205;; Contributed by Kevin Ryde <user42@zip.com.au>. 206(defun c-lineup-argcont (elem) 207 "Line up a continued argument. 208 209foo (xyz, aaa + bbb + ccc 210 + ddd + eee + fff); <- c-lineup-argcont 211 212Only continuation lines like this are touched, nil is returned on lines 213which are the start of an argument. 214 215Within a gcc asm block, \":\" is recognized as an argument separator, 216but of course only between operand specifications, not in the expressions 217for the operands. 218 219Works with: arglist-cont, arglist-cont-nonempty." 220 221 (save-excursion 222 (beginning-of-line) 223 224 (when (eq (car elem) 'arglist-cont-nonempty) 225 ;; Our argument list might not be the innermost one. If it 226 ;; isn't, go back to the last position in it. We do this by 227 ;; stepping back over open parens until we get to the open paren 228 ;; of our argument list. 229 (let ((open-paren (c-langelem-2nd-pos c-syntactic-element)) 230 (paren-state (c-parse-state))) 231 (while (not (eq (car paren-state) open-paren)) 232 (unless (consp (car paren-state)) ;; ignore matched braces 233 (goto-char (car paren-state))) 234 (setq paren-state (cdr paren-state))))) 235 236 (let ((start (point)) c) 237 238 (when (bolp) 239 ;; Previous line ending in a comma means we're the start of an 240 ;; argument. This should quickly catch most cases not for us. 241 ;; This case is only applicable if we're the innermost arglist. 242 (c-backward-syntactic-ws) 243 (setq c (char-before))) 244 245 (unless (eq c ?,) 246 ;; In a gcc asm, ":" on the previous line means the start of an 247 ;; argument. And lines starting with ":" are not for us, don't 248 ;; want them to indent to the preceding operand. 249 (let ((gcc-asm (save-excursion 250 (goto-char start) 251 (c-in-gcc-asm-p)))) 252 (unless (and gcc-asm 253 (or (eq c ?:) 254 (save-excursion 255 (goto-char start) 256 (looking-at "[ \t]*:")))) 257 258 (c-lineup-argcont-scan (if gcc-asm ?:)) 259 (vector (current-column)))))))) 260 261(defun c-lineup-argcont-scan (&optional other-match) 262 ;; Find the start of an argument, for `c-lineup-argcont'. 263 (when (zerop (c-backward-token-2 1 t)) 264 (let ((c (char-after))) 265 (if (or (eq c ?,) (eq c other-match)) 266 (progn 267 (forward-char) 268 (c-forward-syntactic-ws)) 269 (c-lineup-argcont-scan other-match))))) 270 271(defun c-lineup-arglist-intro-after-paren (langelem) 272 "Line up a line to just after the open paren of the surrounding paren 273or brace block. 274 275Works with: defun-block-intro, brace-list-intro, 276statement-block-intro, statement-case-intro, arglist-intro." 277 (save-excursion 278 (beginning-of-line) 279 (backward-up-list 1) 280 (skip-chars-forward " \t" (c-point 'eol)) 281 (vector (1+ (current-column))))) 282 283(defun c-lineup-arglist-close-under-paren (langelem) 284 "Line up a line under the enclosing open paren. 285Normally used to line up a closing paren in the same column as its 286corresponding open paren, but can also be used with arglist-cont and 287arglist-cont-nonempty to line up all lines inside a parenthesis under 288the open paren. 289 290As a special case, if a brace block construct starts at the same line 291as the open parenthesis of the argument list, the indentation is 292`c-basic-offset' only. See `c-lineup-arglist' for further discussion 293of this \"DWIM\" measure. 294 295Works with: Almost all symbols, but are typically most useful on 296arglist-close, brace-list-close, arglist-cont and arglist-cont-nonempty." 297 (save-excursion 298 (if (memq (c-langelem-sym langelem) 299 '(arglist-cont-nonempty arglist-close)) 300 (goto-char (c-langelem-2nd-pos c-syntactic-element)) 301 (beginning-of-line) 302 (c-go-up-list-backward)) 303 304 (if (save-excursion (c-block-in-arglist-dwim (point))) 305 c-basic-offset ; DWIM case. 306 307 ;; Normal case. Indent to the arglist open paren. 308 (let (special-list) 309 (if (and c-special-brace-lists 310 (setq special-list (c-looking-at-special-brace-list))) 311 ;; Cope if we're in the middle of a special brace list 312 ;; opener like "({". 313 (goto-char (car (car special-list)))) 314 (vector (current-column)))))) 315 316(defun c-lineup-arglist-operators (langelem) 317 "Line up lines starting with an infix operator under the open paren. 318Return nil on lines that don't start with an operator, to leave those 319cases to other line-up functions. Example: 320 321if ( x < 10 322 || at_limit (x, <- c-lineup-arglist-operators 323 list) <- c-lineup-arglist-operators returns nil 324 ) 325 326Since this function doesn't do anything for lines without an infix 327operator you typically want to use it together with some other line-up 328settings, e.g. as follows \(the arglist-close setting is just a 329suggestion to get a consistent style): 330 331\(c-set-offset 'arglist-cont '(c-lineup-arglist-operators 0)) 332\(c-set-offset 'arglist-cont-nonempty '(c-lineup-arglist-operators 333 c-lineup-arglist)) 334\(c-set-offset 'arglist-close '(c-lineup-arglist-close-under-paren)) 335 336Works with: arglist-cont, arglist-cont-nonempty." 337 (save-excursion 338 (back-to-indentation) 339 (when (looking-at "[-+|&*%<>=]\\|\\(/[^/*]\\)") 340 ;; '-' can be both an infix and a prefix operator, but I'm lazy now.. 341 (c-lineup-arglist-close-under-paren langelem)))) 342 343(defun c-lineup-close-paren (langelem) 344 "Line up the closing paren under its corresponding open paren if the 345open paren is followed by code. If the open paren ends its line, no 346indentation is added. E.g: 347 348main (int, main ( 349 char ** int, char ** 350 ) <-> ) <- c-lineup-close-paren 351 352As a special case, if a brace block construct starts at the same line 353as the open parenthesis of the argument list, the indentation is 354`c-basic-offset' instead of the open paren column. See 355`c-lineup-arglist' for further discussion of this \"DWIM\" measure. 356 357Works with: All *-close symbols." 358 (save-excursion 359 (if (memq (c-langelem-sym langelem) 360 '(arglist-cont-nonempty arglist-close)) 361 (goto-char (c-langelem-2nd-pos c-syntactic-element)) 362 (beginning-of-line) 363 (c-go-up-list-backward)) 364 365 (let (special-list arglist-start) 366 (if (and c-special-brace-lists 367 (setq special-list (c-looking-at-special-brace-list))) 368 ;; Cope if we're in the middle of a special brace list 369 ;; opener like "({". 370 (progn 371 (goto-char (setq arglist-start (car (car special-list)))) 372 (c-forward-token-2) 373 (forward-char)) 374 (setq arglist-start (point)) 375 (forward-char)) 376 377 (cond ((looking-at c-syntactic-eol) 378 0) ; The arglist is "empty". 379 380 ((c-block-in-arglist-dwim (point)) 381 c-basic-offset) ; DWIM case. 382 383 (t 384 ;; Normal case. Indent to the arglist open paren. 385 (goto-char arglist-start) 386 (vector (current-column))))))) 387 388(defun c-lineup-streamop (langelem) 389 "Line up C++ stream operators under each other. 390 391Works with: stream-op." 392 (save-excursion 393 (goto-char (c-langelem-pos langelem)) 394 (re-search-forward "<<\\|>>" (c-point 'eol) 'move) 395 (goto-char (match-beginning 0)) 396 (vector (current-column)))) 397 398(defun c-lineup-multi-inher (langelem) 399 "Line up the classes in C++ multiple inheritance clauses and member 400initializers under each other. E.g: 401 402class Foo: Foo::Foo (int a, int b): 403 public Cyphr, Cyphr (a), 404 public Bar <-> Bar (b) <- c-lineup-multi-inher 405 406class Foo Foo::Foo (int a, int b) 407 : public Cyphr, : Cyphr (a), 408 public Bar <-> Bar (b) <- c-lineup-multi-inher 409 410class Foo Foo::Foo (int a, int b) 411 : public Cyphr : Cyphr (a) 412 , public Bar <-> , Bar (b) <- c-lineup-multi-inher 413 414Works with: inher-cont, member-init-cont." 415 (save-excursion 416 (back-to-indentation) 417 (let* ((eol (c-point 'eol)) 418 (here (point)) 419 (char-after-ip (char-after))) 420 (if (c-langelem-pos langelem) 421 (goto-char (c-langelem-pos langelem))) 422 423 ;; This kludge is necessary to support both inher-cont and 424 ;; member-init-cont, since they have different anchor positions. 425 (c-backward-syntactic-ws) 426 (when (eq (char-before) ?:) 427 (backward-char) 428 (c-backward-syntactic-ws)) 429 430 (c-syntactic-re-search-forward ":" eol 'move) 431 (if (looking-at c-syntactic-eol) 432 (c-forward-syntactic-ws here) 433 (if (eq char-after-ip ?,) 434 (backward-char) 435 (skip-chars-forward " \t" eol))) 436 (if (< (point) here) 437 (vector (current-column))) 438 ))) 439 440(defun c-lineup-java-inher (langelem) 441 "Line up Java implements and extends declarations. 442If class names follow on the same line as the implements/extends 443keyword, they are lined up under each other. Otherwise, they are 444indented by adding `c-basic-offset' to the column of the keyword. 445E.g: 446 447class Foo class Foo 448 extends extends Cyphr, 449 Bar <-> Bar <- c-lineup-java-inher 450 <--> c-basic-offset 451 452Works with: inher-cont." 453 (save-excursion 454 (goto-char (c-langelem-pos langelem)) 455 (forward-word 1) 456 (if (looking-at "[ \t]*$") 457 c-basic-offset 458 (c-forward-syntactic-ws) 459 (vector (current-column))))) 460 461(defun c-lineup-java-throws (langelem) 462 "Line up Java throws declarations. 463If exception names follow on the same line as the throws keyword, 464they are lined up under each other. Otherwise, they are indented by 465adding `c-basic-offset' to the column of the throws keyword. The 466throws keyword itself is also indented by `c-basic-offset' from the 467function declaration start if it doesn't hang. E.g: 468 469int foo() int foo() throws Cyphr, 470 throws <-> Bar, <- c-lineup-java-throws 471 Bar <-> Vlod <- c-lineup-java-throws 472<--><--> c-basic-offset 473 474Works with: func-decl-cont." 475 (save-excursion 476 (let* ((lim (1- (c-point 'bol))) 477 (throws (catch 'done 478 (goto-char (c-langelem-pos langelem)) 479 (while (zerop (c-forward-token-2 1 t lim)) 480 (if (looking-at "throws\\>[^_]") 481 (throw 'done t)))))) 482 (if throws 483 (if (zerop (c-forward-token-2 1 nil (c-point 'eol))) 484 (vector (current-column)) 485 (back-to-indentation) 486 (vector (+ (current-column) c-basic-offset))) 487 c-basic-offset)))) 488 489(defun c-indent-one-line-block (langelem) 490 "Indent a one line block `c-basic-offset' extra. 491E.g: 492 493if (n > 0) if (n > 0) 494 {m+=n; n=0;} <-> { <- c-indent-one-line-block 495<--> c-basic-offset m+=n; n=0; 496 } 497 498The block may use any kind of parenthesis character. nil is returned 499if the line doesn't start with a one line block, which makes the 500function usable in list expressions. 501 502Work with: Almost all syntactic symbols, but most useful on *-open." 503 (save-excursion 504 (let ((eol (c-point 'eol))) 505 (back-to-indentation) 506 (if (and (eq (char-syntax (char-after)) ?\() 507 (c-safe (progn (c-forward-sexp) t)) 508 (<= (point) eol)) 509 c-basic-offset 510 nil)))) 511 512(defun c-indent-multi-line-block (langelem) 513 "Indent a multi line block `c-basic-offset' extra. 514E.g: 515 516int *foo[] = { int *foo[] = { 517 NULL, NULL, 518 {17}, <-> { <- c-indent-multi-line-block 519 17 520 } 521 <--> c-basic-offset 522 523The block may use any kind of parenthesis character. nil is returned 524if the line doesn't start with a multi line block, which makes the 525function usable in list expressions. 526 527Work with: Almost all syntactic symbols, but most useful on *-open." 528 (save-excursion 529 (let ((eol (c-point 'eol))) 530 (back-to-indentation) 531 (if (and (eq (char-syntax (char-after)) ?\() 532 (or (not (c-safe (progn (c-forward-sexp) t))) 533 (> (point) eol))) 534 c-basic-offset 535 nil)))) 536 537(defun c-lineup-C-comments (langelem) 538 "Line up C block comment continuation lines. 539Various heuristics are used to handle many of the common comment 540styles. Some examples: 541 542/* /** /* /* text /* /** 543 * text * text text text ** text ** text 544 */ */ */ */ */ */ 545 546/********************************************************************* 547 * text 548 ********************************************************************/ 549 550/********************************************************************* 551 Free form text comments: 552 In comments with a long delimiter line at the start, the indentation 553 is kept unchanged for lines that start with an empty comment line 554 prefix. The delimiter line is whatever matches the 555 `comment-start-skip' regexp. 556*********************************************************************/ 557 558The variable `c-comment-prefix-regexp' is used to recognize the 559comment line prefix, e.g. the `*' that usually starts every line 560inside a comment. 561 562Works with: The `c' syntactic symbol." 563 (save-excursion 564 (let* ((here (point)) 565 (prefixlen (progn (back-to-indentation) 566 (if (looking-at c-current-comment-prefix) 567 (- (match-end 0) (point)) 568 0))) 569 (starterlen 570 ;; Get the length of the comment starter, not including 571 ;; the first '/'. We check if the comment prefix matched 572 ;; on the current line matches the starter or if it 573 ;; matches comment-start-skip, and choose whichever is 574 ;; longest. 575 (max (save-excursion 576 (goto-char (1+ (c-langelem-pos langelem))) 577 (if (and (match-string 0) 578 (looking-at (regexp-quote (match-string 0)))) 579 (- (match-end 0) (match-beginning 0)) 580 0)) 581 (save-excursion 582 (goto-char (c-langelem-pos langelem)) 583 (looking-at comment-start-skip) 584 (- (or (match-end 1) 585 (save-excursion 586 (goto-char (match-end 0)) 587 (skip-chars-backward " \t") 588 (point))) 589 (point) 590 1))))) 591 (if (and (> starterlen 10) (zerop prefixlen)) 592 ;; The comment has a long starter and the line doesn't have 593 ;; a nonempty comment prefix. Treat it as free form text 594 ;; and don't change the indentation. 595 (vector (current-column)) 596 ;; Go back to the previous non-blank line, if any. 597 (while 598 (progn 599 (forward-line -1) 600 (back-to-indentation) 601 (and (> (point) (c-langelem-pos langelem)) 602 (looking-at "[ \t]*$")))) 603 ;; Is the starting line the first continuation line with content? 604 (if (>= (c-langelem-pos langelem) (point)) 605 (if (zerop prefixlen) 606 ;; No nonempty comment prefix. Align after comment 607 ;; starter. 608 (progn 609 (looking-at comment-start-skip) 610 (goto-char (match-end 0)) 611 ;; The following should not be necessary, since 612 ;; comment-start-skip should match everything (i.e. 613 ;; typically whitespace) that leads up to the text. 614 ;;(if (looking-at "\\([ \t]+\\).+$") 615 ;; ;; Align with the text that hangs after the 616 ;; ;; comment starter. 617 ;; (goto-char (match-end 1))) 618 (vector (current-column))) 619 ;; How long is the comment starter? if greater than the 620 ;; length of the comment prefix, align left. if less 621 ;; than or equal, align right. this should also pick up 622 ;; Javadoc style comments. 623 (if (> starterlen prefixlen) 624 (progn 625 (goto-char (c-langelem-pos langelem)) 626 (vector (1+ (current-column)))) 627 (goto-char (+ (c-langelem-pos langelem) starterlen 1)) 628 (vector (- (current-column) prefixlen)))) 629 ;; We didn't start on the first non-blank continuation line. If the 630 ;; previous line has a nonempty comment prefix, align with it. 631 ;; Otherwise, align with the previous nonempty line, but align the 632 ;; comment ender with the starter. 633 (when (or (not (looking-at c-current-comment-prefix)) 634 (eq (match-beginning 0) (match-end 0))) 635 (goto-char here) 636 (back-to-indentation) 637 (if (looking-at (concat "\\(" c-current-comment-prefix "\\)\\*/")) 638 (goto-char (c-langelem-pos langelem)) 639 (while (and (zerop (forward-line -1)) 640 (looking-at "^[ \t]*$"))) 641 (back-to-indentation) 642 (if (< (point) (c-langelem-pos langelem)) 643 ;; Align with the comment starter rather than 644 ;; with the code before it. 645 (goto-char (c-langelem-pos langelem))))) 646 (vector (current-column))))))) 647 648(defun c-lineup-comment (langelem) 649 "Line up a comment start according to `c-comment-only-line-offset'. 650If the comment is lined up with a comment starter on the previous 651line, that alignment is preserved. 652 653Works with: comment-intro." 654 (save-excursion 655 (back-to-indentation) 656 (let ((col (current-column))) 657 (cond 658 ;; CASE 1: preserve aligned comments 659 ((save-excursion 660 (and (c-backward-single-comment) 661 (= col (current-column)))) 662 (vector col)) ; Return an absolute column. 663 ;; indent as specified by c-comment-only-line-offset 664 ((not (bolp)) 665 (or (car-safe c-comment-only-line-offset) 666 c-comment-only-line-offset)) 667 (t 668 (or (cdr-safe c-comment-only-line-offset) 669 (car-safe c-comment-only-line-offset) 670 -1000)) ;jam it against the left side 671 )))) 672 673(defun c-lineup-knr-region-comment (langelem) 674 "Line up a comment in the \"K&R region\" with the declaration. 675That is the region between the function or class header and the 676beginning of the block. E.g: 677 678int main() 679/* This is the main function. */ <- c-lineup-knr-region-comment 680{ 681 return 0; 682} 683 684Return nil if called in any other situation, to be useful in list 685expressions. 686 687Works with: comment-intro." 688 (when (or (assq 'topmost-intro-cont c-syntactic-context) 689 (assq 'func-decl-cont c-syntactic-context) 690 (assq 'knr-argdecl-intro c-syntactic-context) 691 (assq 'lambda-intro-cont c-syntactic-context)) 692 (save-excursion 693 (beginning-of-line) 694 (c-beginning-of-statement-1) 695 (vector (current-column))))) 696 697(defun c-lineup-runin-statements (langelem) 698 "Line up statements when the first statement is on the same line as 699the block opening brace. E.g: 700 701int main() 702{ puts (\"Hello world!\"); 703 return 0; <- c-lineup-runin-statements 704} 705 706If there is no statement after the opening brace to align with, nil is 707returned. This makes the function usable in list expressions. 708 709Works with: The `statement' syntactic symbol." 710 (if (eq (char-after (c-langelem-pos langelem)) ?{) 711 (save-excursion 712 (if (c-langelem-pos langelem) 713 (goto-char (c-langelem-pos langelem))) 714 (forward-char 1) 715 (skip-chars-forward " \t") 716 (unless (eolp) 717 (vector (current-column)))))) 718 719(defun c-lineup-assignments (langelem) 720 "Line up the current line after the assignment operator on the first 721line in the statement. If there isn't any, return nil to allow 722stacking with other line-up functions. If the current line contains 723an assignment operator too, try to align it with the first one. 724 725Works with: topmost-intro-cont, statement-cont, arglist-cont, 726arglist-cont-nonempty." 727 (let (startpos endpos equalp) 728 729 (if (eq (c-langelem-sym langelem) 'arglist-cont-nonempty) 730 ;; If it's an arglist-cont-nonempty then we're only interested 731 ;; in equal signs outside it. We don't search for a "=" on 732 ;; the current line since that'd have a different nesting 733 ;; compared to the one we should align with. 734 (save-excursion 735 (save-restriction 736 (setq endpos (c-langelem-2nd-pos c-syntactic-element)) 737 (narrow-to-region (c-langelem-pos langelem) endpos) 738 (if (setq startpos (c-up-list-backward endpos)) 739 (setq startpos (1+ startpos)) 740 (setq startpos (c-langelem-pos langelem))))) 741 742 (setq startpos (c-langelem-pos langelem) 743 endpos (point)) 744 745 ;; Find a syntactically relevant and unnested "=" token on the 746 ;; current line. equalp is in that case set to the number of 747 ;; columns to left shift the current line to align it with the 748 ;; goal column. 749 (save-excursion 750 (beginning-of-line) 751 (when (c-syntactic-re-search-forward 752 c-assignment-op-regexp 753 (c-point 'eol) t t t) 754 (setq equalp (- (or (match-beginning 1) 755 (match-end 0)) 756 (c-point 'boi)))))) 757 758 (save-excursion 759 (goto-char startpos) 760 (if (or (if (c-syntactic-re-search-forward 761 c-assignment-op-regexp 762 (min endpos (c-point 'eol)) t t t) 763 (progn 764 (goto-char (or (match-beginning 1) 765 (match-end 0))) 766 nil) 767 t) 768 (save-excursion 769 (c-forward-syntactic-ws (c-point 'eol)) 770 (eolp))) 771 ;; There's no equal sign on the line, or there is one but 772 ;; nothing follows it. 773 nil 774 775 ;; calculate indentation column after equals and ws, unless 776 ;; our line contains an equals sign 777 (if (not equalp) 778 (progn 779 (skip-chars-forward " \t") 780 (setq equalp 0))) 781 782 (vector (- (current-column) equalp))) 783 ))) 784 785(defun c-lineup-math (langelem) 786 "Like `c-lineup-assignments' but indent with `c-basic-offset' if no 787assignment operator was found on the first line. I.e. this function 788is the same as specifying a list (c-lineup-assignments +). It's 789provided for compatibility with old configurations. 790 791Works with: topmost-intro-cont, statement-cont, arglist-cont, 792arglist-cont-nonempty." 793 (or (c-lineup-assignments langelem) 794 c-basic-offset)) 795 796(defun c-lineup-cascaded-calls (langelem) 797 "Line up \"cascaded calls\" under each other. 798If the line begins with \"->\" or \".\" and the preceding line ends 799with one or more function calls preceded by the same token, then the 800arrow is lined up with the first of those tokens. E.g: 801 802result = proc->add(17)->add(18) 803 ->add(19) + <- c-lineup-cascaded-calls 804 offset; <- c-lineup-cascaded-calls (inactive) 805 806In any other situation nil is returned to allow use in list 807expressions. 808 809Works with: topmost-intro-cont, statement-cont, arglist-cont, 810arglist-cont-nonempty." 811 812 (if (and (eq (c-langelem-sym langelem) 'arglist-cont-nonempty) 813 (not (eq (c-langelem-2nd-pos c-syntactic-element) 814 (c-most-enclosing-brace (c-parse-state))))) 815 ;; The innermost open paren is not our one, so don't do 816 ;; anything. This can occur for arglist-cont-nonempty with 817 ;; nested arglist starts on the same line. 818 nil 819 820 (save-excursion 821 (back-to-indentation) 822 (let ((operator (and (looking-at "->\\|\\.") 823 (regexp-quote (match-string 0)))) 824 (stmt-start (c-langelem-pos langelem)) col) 825 826 (when (and operator 827 (looking-at operator) 828 (zerop (c-backward-token-2 1 t stmt-start)) 829 (eq (char-after) ?\() 830 (zerop (c-backward-token-2 2 t stmt-start)) 831 (looking-at operator)) 832 (setq col (current-column)) 833 834 (while (and (zerop (c-backward-token-2 1 t stmt-start)) 835 (eq (char-after) ?\() 836 (zerop (c-backward-token-2 2 t stmt-start)) 837 (looking-at operator)) 838 (setq col (current-column))) 839 840 (vector col)))))) 841 842(defun c-lineup-string-cont (langelem) 843 "Line up a continued string under the one it continues. 844A continued string in this sense is where a string literal follows 845directly after another one. E.g: 846 847result = prefix + \"A message \" 848 \"string.\"; <- c-lineup-string-cont 849 850In other situations, returns nil, to allow stacking with other 851line-up functions. 852 853Works with: topmost-intro-cont, statement-cont, arglist-cont, 854arglist-cont-nonempty." 855 (save-excursion 856 (back-to-indentation) 857 (and (looking-at "\\s\"") 858 (let ((quote (char-after)) pos) 859 (while (and (progn (c-backward-syntactic-ws) 860 (eq (char-before) quote)) 861 (c-safe (c-backward-sexp) t) 862 (/= (setq pos (point)) (c-point 'boi)))) 863 (when pos 864 (goto-char pos) 865 (vector (current-column))))))) 866 867(defun c-lineup-template-args (langelem) 868 "Line up template argument lines under the first argument. 869To allow this function to be used in a list expression, nil is 870returned if there's no template argument on the first line. 871 872Works with: template-args-cont." 873 (save-excursion 874 (c-with-syntax-table c++-template-syntax-table 875 (beginning-of-line) 876 (backward-up-list 1) 877 (if (and (eq (char-after) ?<) 878 (zerop (c-forward-token-2 1 nil (c-point 'eol)))) 879 (vector (current-column)))))) 880 881(defun c-lineup-ObjC-method-call (langelem) 882 "Line up selector args as Emacs Lisp mode does with function args: 883Go to the position right after the message receiver, and if you are at 884the end of the line, indent the current line c-basic-offset columns 885from the opening bracket; otherwise you are looking at the first 886character of the first method call argument, so line up the current 887line with it. 888 889Works with: objc-method-call-cont." 890 (save-excursion 891 (let* ((extra (save-excursion 892 (back-to-indentation) 893 (c-backward-syntactic-ws (c-langelem-pos langelem)) 894 (if (eq (char-before) ?:) 895 (- c-basic-offset) 896 0))) 897 (open-bracket-pos (c-langelem-pos langelem)) 898 (open-bracket-col (progn 899 (goto-char open-bracket-pos) 900 (current-column))) 901 (target-col (progn 902 (forward-char) 903 (c-forward-sexp) 904 (skip-chars-forward " \t") 905 (if (eolp) 906 (+ open-bracket-col c-basic-offset) 907 (current-column)))) 908 ) 909 (- target-col open-bracket-col extra)))) 910 911(defun c-lineup-ObjC-method-args (langelem) 912 "Line up the colons that separate args. 913The colon on the current line is aligned with the one on the first 914line. 915 916Works with: objc-method-args-cont." 917 (save-excursion 918 (let* ((here (c-point 'boi)) 919 (curcol (progn (goto-char here) (current-column))) 920 (eol (c-point 'eol)) 921 (relpos (c-langelem-pos langelem)) 922 (first-col-column (progn 923 (goto-char relpos) 924 (skip-chars-forward "^:" eol) 925 (and (eq (char-after) ?:) 926 (current-column))))) 927 (if (not first-col-column) 928 c-basic-offset 929 (goto-char here) 930 (skip-chars-forward "^:" eol) 931 (if (eq (char-after) ?:) 932 (+ curcol (- first-col-column (current-column))) 933 c-basic-offset))))) 934 935(defun c-lineup-ObjC-method-args-2 (langelem) 936 "Line up the colons that separate args. 937The colon on the current line is aligned with the one on the previous 938line. 939 940Works with: objc-method-args-cont." 941 (save-excursion 942 (let* ((here (c-point 'boi)) 943 (curcol (progn (goto-char here) (current-column))) 944 (eol (c-point 'eol)) 945 (relpos (c-langelem-pos langelem)) 946 (prev-col-column (progn 947 (skip-chars-backward "^:" relpos) 948 (and (eq (char-before) ?:) 949 (- (current-column) 1))))) 950 (if (not prev-col-column) 951 c-basic-offset 952 (goto-char here) 953 (skip-chars-forward "^:" eol) 954 (if (eq (char-after) ?:) 955 (+ curcol (- prev-col-column (current-column))) 956 c-basic-offset))))) 957 958(defun c-lineup-inexpr-block (langelem) 959 "Line up the block for constructs that use a block inside an expression, 960e.g. anonymous classes in Java and lambda functions in Pike. The body 961is aligned with the start of the header, e.g. with the \"new\" or 962\"lambda\" keyword. Returns nil if the block isn't part of such a 963construct. 964 965Works with: inlambda, inexpr-statement, inexpr-class." 966 (save-excursion 967 (back-to-indentation) 968 (let* ((paren-state (c-parse-state)) 969 (containing-sexp (c-most-enclosing-brace paren-state)) 970 (res (or (c-looking-at-inexpr-block 971 (c-safe-position containing-sexp paren-state) 972 containing-sexp) 973 (and containing-sexp 974 (progn (goto-char containing-sexp) 975 (eq (char-after) ?{)) 976 (progn (setq containing-sexp 977 (c-most-enclosing-brace paren-state 978 (point))) 979 (c-looking-at-inexpr-block 980 (c-safe-position containing-sexp paren-state) 981 containing-sexp)))))) 982 (when res 983 (goto-char (cdr res)) 984 (vector (current-column)))))) 985 986(defun c-lineup-whitesmith-in-block (langelem) 987 "Line up lines inside a block in Whitesmith style. 988It's done in a way that works both when the opening brace hangs and 989when it doesn't. E.g: 990 991something 992 { something { 993 foo; <-> foo; <- c-lineup-whitesmith-in-block 994 } } 995 <--> c-basic-offset 996 997In the first case the indentation is kept unchanged, in the 998second `c-basic-offset' is added. 999 1000Works with: defun-close, defun-block-intro, inline-close, block-close, 1001brace-list-close, brace-list-intro, statement-block-intro, 1002arglist-intro, arglist-cont-nonempty, arglist-close, and all in* 1003symbols, e.g. inclass and inextern-lang." 1004 (save-excursion 1005 (if (and (c-go-up-list-backward) 1006 (= (point) (c-point 'boi))) 1007 nil 1008 c-basic-offset))) 1009 1010(defun c-lineup-after-whitesmith-blocks (langelem) 1011 "Compensate for Whitesmith style indentation of blocks. 1012Due to the way CC Mode calculates anchor positions for normal lines 1013inside blocks, this function is necessary for those lines to get 1014correct Whitesmith style indentation. Consider the following 1015examples: 1016 1017 int foo() 1018 { 1019int foo() { 1020 { a; 1021 a; } 1022 x; <-> x; <- c-lineup-after-whitesmith-blocks 1023 1024The fact that the line with \"x\" is preceded by a Whitesmith style 1025indented block in one case and not the other should not affect its 1026indentation. But since CC Mode in cases like this uses the 1027indentation of the preceding statement as anchor position, the \"x\" 1028would in the rightmost case be indented too much if the offset for 1029`statement' was set simply to zero. 1030 1031This lineup function corrects for this situation by detecting if the 1032anchor position is at an open paren character. In that case, it 1033instead indents relative to the surrounding block just like 1034`c-lineup-whitesmith-in-block'. 1035 1036Works with: brace-list-entry, brace-entry-open, statement, 1037arglist-cont." 1038 (save-excursion 1039 (goto-char (c-langelem-pos langelem)) 1040 (when (looking-at "\\s\(") 1041 (if (c-go-up-list-backward) 1042 (let ((pos (point))) 1043 (back-to-indentation) 1044 (if (= pos (point)) 1045 (vector (current-column)) 1046 (vector (+ (current-column) c-basic-offset)))) 1047 (vector 0))))) 1048 1049(defun c-lineup-cpp-define (langelem) 1050 "Line up macro continuation lines according to the indentation of 1051the construct preceding the macro. E.g: 1052 1053v beg of preceding constr v beg of preceding constr 1054 int dribble() { 1055const char msg[] = if (!running) 1056 \"Some text.\"; error(\"Not running!\"); 1057 1058#define X(A, B) \ #define X(A, B) \ 1059do { \ <-> do { \ <- c-lineup-cpp-define 1060 printf (A, B); \ printf (A, B); \ 1061} while (0) } while (0) 1062 1063If `c-syntactic-indentation-in-macros' is non-nil, the function 1064returns the relative indentation to the macro start line to allow 1065accumulation with other offsets. E.g. in the following cases, 1066cpp-define-intro is combined with the statement-block-intro that comes 1067from the \"do {\" that hangs on the \"#define\" line: 1068 1069 int dribble() { 1070const char msg[] = if (!running) 1071 \"Some text.\"; error(\"Not running!\"); 1072 1073#define X(A, B) do { \ #define X(A, B) do { \ 1074 printf (A, B); \ <-> printf (A, B); \ <- c-lineup-cpp-define 1075 this->refs++; \ this->refs++; \ 1076} while (0) <-> } while (0) <- c-lineup-cpp-define 1077 1078The relative indentation returned by `c-lineup-cpp-define' is zero and 1079two, respectively, in these two examples. They are then added to the 1080two column indentation that statement-block-intro gives in both cases 1081here. 1082 1083If the relative indentation is zero, then nil is returned instead. 1084That is useful in a list expression to specify the default indentation 1085on the top level. 1086 1087If `c-syntactic-indentation-in-macros' is nil then this function keeps 1088the current indentation, except for empty lines \(ignoring the ending 1089backslash) where it takes the indentation from the closest preceding 1090nonempty line in the macro. If there's no such line in the macro then 1091the indentation is taken from the construct preceding it, as described 1092above. 1093 1094Works with: cpp-define-intro." 1095 (let (offset) 1096 (if c-syntactic-indentation-in-macros 1097 ;; Go to the macro start and do a syntactic analysis of it. 1098 ;; Then remove the cpp-macro element it should contain and 1099 ;; calculate the indentation it then would get. 1100 (save-excursion 1101 (c-beginning-of-macro) 1102 (setq offset (- (c-get-syntactic-indentation 1103 (delete '(cpp-macro) (c-guess-basic-syntax))) 1104 (save-excursion 1105 (back-to-indentation) 1106 (current-column)))) 1107 (if (zerop offset) 1108 nil 1109 offset)) 1110 ;; Do not indent syntactically inside the macro. 1111 (save-excursion 1112 (let ((macro-start-line (save-excursion 1113 (goto-char (c-query-macro-start)) 1114 (beginning-of-line) 1115 (point)))) 1116 (beginning-of-line) 1117 ;; Check every line while inside the macro. 1118 (while (and (> (point) macro-start-line) 1119 (looking-at "[ \t]*\\\\?$") 1120 (= (forward-line -1) 0))) 1121 (if (<= (point) macro-start-line) 1122 ;; If we've stepped out of the macro we take the 1123 ;; syntactic offset. 1124 (setq offset (c-get-syntactic-indentation 1125 (delete '(cpp-macro) (c-guess-basic-syntax)))) 1126 (setq offset (current-indentation))) 1127 (if (zerop offset) 1128 nil 1129 (vector offset))))))) 1130 1131;; Contributed by Kevin Ryde <user42@zip.com.au>. 1132(defun c-lineup-gcc-asm-reg (elem) 1133 "Line up a gcc asm register under one on a previous line. 1134 1135 asm (\"foo %1, %0\\n\" 1136 \"bar %0, %1\" 1137 : \"=r\" (w), 1138 \"=r\" (x) 1139 : \"0\" (y), 1140 \"1\" (z)); 1141 1142The \"x\" line is aligned to the text after the \":\" on the \"w\" line, and 1143similarly \"z\" under \"y\". 1144 1145This is done only in an \"asm\" or \"__asm__\" block, and only to 1146those lines mentioned. Anywhere else nil is returned. The usual 1147arrangement is to have this routine as an extra feature at the start 1148of arglist line-ups, e.g. 1149 1150 (c-lineup-gcc-asm-reg c-lineup-arglist) 1151 1152Works with: arglist-cont, arglist-cont-nonempty." 1153 1154 (let ((orig-pos (point)) 1155 alignto) 1156 (save-excursion 1157 (and 1158 c-opt-asm-stmt-key 1159 1160 ;; Don't do anything if the innermost open paren isn't our one. 1161 ;; This can occur for arglist-cont-nonempty with nested arglist 1162 ;; starts on the same line. 1163 (or (not (eq (car elem) 'arglist-cont-nonempty)) 1164 (eq (c-langelem-2nd-pos c-syntactic-element) 1165 (c-most-enclosing-brace (c-parse-state)))) 1166 1167 ;; Find the ":" to align to. Look for this first so as to quickly 1168 ;; eliminate pretty much all cases which are not for us. 1169 (re-search-backward "^[ \t]*:[ \t]*\\(.\\)?" (cdr elem) t) 1170 1171 ;; Must have something after the ":". 1172 (setq alignto (match-beginning 1)) 1173 1174 ;; Don't touch ":" lines themselves. 1175 (progn (goto-char orig-pos) 1176 (beginning-of-line) 1177 (not (looking-at "^[ \t]*:"))) 1178 1179 ;; Only operate in an asm statement. 1180 (progn (goto-char orig-pos) 1181 (c-in-gcc-asm-p)) 1182 1183 (vector (progn (goto-char alignto) (current-column))))))) 1184 1185(defun c-lineup-dont-change (langelem) 1186 "Do not change the indentation of the current line. 1187 1188Works with: Any syntactic symbol." 1189 (save-excursion 1190 (back-to-indentation) 1191 (vector (current-column)))) 1192 1193 1194(defun c-snug-do-while (syntax pos) 1195 "Dynamically calculate brace hanginess for do-while statements. 1196Using this function, `while' clauses that end a `do-while' block will 1197remain on the same line as the brace that closes that block. 1198 1199See `c-hanging-braces-alist' for how to utilize this function as an 1200ACTION associated with `block-close' syntax." 1201 (save-excursion 1202 (let (langelem) 1203 (if (and (eq syntax 'block-close) 1204 (setq langelem (assq 'block-close c-syntactic-context)) 1205 (progn (goto-char (c-langelem-pos langelem)) 1206 (if (eq (char-after) ?{) 1207 (c-safe (c-forward-sexp -1))) 1208 (looking-at "\\<do\\>[^_]"))) 1209 '(before) 1210 '(before after))))) 1211 1212(defun c-snug-1line-defun-close (syntax pos) 1213 "Determine the brace hanginess for an AWK defun-close. 1214If the action/function being closed is a one-liner, keep it so. Otherwise put 1215the closing brace on its own line." 1216 (save-excursion 1217 (goto-char pos) 1218 (if (> (c-point 'bol) 1219 (progn (up-list -1) (point))) 1220 '(before after) 1221 '(after)))) 1222 1223(defun c-gnu-impose-minimum () 1224 "Imposes a minimum indentation for lines inside code blocks. 1225The variable `c-label-minimum-indentation' specifies the minimum 1226indentation amount." 1227 1228 (when (and (not 1229 ;; Don't adjust macro or comment-only lines. 1230 (or (assq 'cpp-macro c-syntactic-context) 1231 (assq 'comment-intro c-syntactic-context))) 1232 (c-intersect-lists c-inside-block-syms c-syntactic-context) 1233 (save-excursion 1234 (back-to-indentation) 1235 (< (current-column) c-label-minimum-indentation))) 1236 (c-shift-line-indentation (- c-label-minimum-indentation 1237 (current-indentation))))) 1238 1239 1240;; Useful for c-hanging-semi&comma-criteria 1241 1242(defun c-semi&comma-inside-parenlist () 1243 "Controls newline insertion after semicolons in parenthesis lists. 1244If a comma was inserted, no determination is made. If a semicolon was 1245inserted inside a parenthesis list, no newline is added otherwise a 1246newline is added. In either case, checking is stopped. This supports 1247exactly the old newline insertion behavior." 1248 ;; newline only after semicolon, but only if that semicolon is not 1249 ;; inside a parenthesis list (e.g. a for loop statement) 1250 (if (not (eq last-command-char ?\;)) 1251 nil ; continue checking 1252 (if (condition-case nil 1253 (save-excursion 1254 (up-list -1) 1255 (not (eq (char-after) ?\())) 1256 (error t)) 1257 t 1258 'stop))) 1259 1260;; Suppresses newlines before non-blank lines 1261(defun c-semi&comma-no-newlines-before-nonblanks () 1262 "Controls newline insertion after semicolons. 1263If a comma was inserted, no determination is made. If a semicolon was 1264inserted, and the following line is not blank, no newline is inserted. 1265Otherwise, no determination is made." 1266 (save-excursion 1267 (if (and (= last-command-char ?\;) 1268 ;;(/= (point-max) 1269 ;; (save-excursion (skip-syntax-forward " ") (point)) 1270 (zerop (forward-line 1)) 1271 (bolp) ; forward-line has funny behavior at eob. 1272 (not (looking-at "^[ \t]*$"))) 1273 'stop 1274 nil))) 1275 1276;; Suppresses new lines after semicolons in one-liners methods 1277(defun c-semi&comma-no-newlines-for-oneline-inliners () 1278 "Controls newline insertion after semicolons for some one-line methods. 1279If a comma was inserted, no determination is made. Newlines are 1280suppressed in one-liners, if the line is an in-class inline function. 1281For other semicolon contexts, no determination is made." 1282 (let ((syntax (c-guess-basic-syntax)) 1283 (bol (save-excursion 1284 (if (c-safe (up-list -1) t) 1285 (c-point 'bol) 1286 -1)))) 1287 (if (and (eq last-command-char ?\;) 1288 (eq (car (car syntax)) 'inclass) 1289 (eq (car (car (cdr syntax))) 'topmost-intro) 1290 (= (c-point 'bol) bol)) 1291 'stop 1292 nil))) 1293 1294 1295(cc-provide 'cc-align) 1296 1297;;; arch-tag: 4d71ed28-bf51-4509-a148-f39669669a2e 1298;;; cc-align.el ends here 1299