diff options
Diffstat (limited to 'lisp/progmodes/cc-align.el')
-rw-r--r-- | lisp/progmodes/cc-align.el | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/lisp/progmodes/cc-align.el b/lisp/progmodes/cc-align.el index f30477dc787..7884d4bd2ec 100644 --- a/lisp/progmodes/cc-align.el +++ b/lisp/progmodes/cc-align.el @@ -790,6 +790,38 @@ arglist-cont-nonempty." (or (c-lineup-assignments langelem) c-basic-offset)) +(defun c-lineup-ternary-bodies (langelem) + "Line up true and false branches of a ternary operator (i.e. `?:'). +More precisely, if the line starts with a colon which is a part of +a said operator, align it with corresponding question mark; otherwise +return nil. For example: + + return arg % 2 == 0 ? arg / 2 + : (3 * arg + 1); <- c-lineup-ternary-bodies + +Works with: arglist-cont, arglist-cont-nonempty and statement-cont." + (save-excursion + (back-to-indentation) + (when (and (eq ?: (char-after)) + (not (eq ?: (char-after (1+ (point)))))) + (let ((limit (c-langelem-pos langelem)) (depth 1)) + (catch 'done + (while (and (c-syntactic-skip-backward "^?:" limit t) + (not (bobp))) + (backward-char) + (cond ((eq (char-after) ??) + ;; If we've found a question mark, decrease depth. If we've + ;; reached zero, we've found the one we were looking for. + (when (zerop (setq depth (1- depth))) + (throw 'done (vector (current-column))))) + ((or (eq ?: (char-before)) (eq ?? (char-before))) + ;; Step over `::' and `?:' operators. We don't have to + ;; handle `?:' here but doing so saves an iteration. + (if (eq (point) limit) + (throw 'done nil) + (goto-char (1- (point))))) + ((setq depth (1+ depth)))))))))) ; Otherwise increase depth. + (defun c-lineup-cascaded-calls (langelem) "Line up \"cascaded calls\" under each other. If the line begins with \"->\" or \".\" and the preceding line ends @@ -1083,7 +1115,7 @@ arglist-cont." (vector (+ (current-column) c-basic-offset)))) (vector 0))))) -(defun c-lineup-2nd-brace-entry-in-arglist (langelem) +(defun c-lineup-2nd-brace-entry-in-arglist (_langelem) "Lineup the second entry of a brace block under the first, when the first line is also contained in an arglist or an enclosing brace ON THAT LINE. @@ -1124,7 +1156,7 @@ Works with brace-list-intro." (eq (char-after) ?{)))) 'c-lineup-arglist-intro-after-paren)) -(defun c-lineup-class-decl-init-+ (langelem) +(defun c-lineup-class-decl-init-+ (_langelem) "Line up the second entry of a class (etc.) initializer c-basic-offset characters in from the identifier when: \(i) The type is a class, struct, union, etc. (but not an enum); @@ -1165,7 +1197,7 @@ Works with: brace-list-intro." (eq (point) init-pos) (vector (+ (current-column) c-basic-offset))))))) -(defun c-lineup-class-decl-init-after-brace (langelem) +(defun c-lineup-class-decl-init-after-brace (_langelem) "Line up the second entry of a class (etc.) initializer after its opening brace when: \(i) The type is a class, struct, union, etc. (but not an enum); |