summaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authorMichal Nazarewicz <mina86@mina86.com>2020-05-03 16:32:47 +0100
committerMichal Nazarewicz <mina86@mina86.com>2020-05-09 11:30:32 +0100
commitfab23328512e47a50caced8d074e86e583cc8a9f (patch)
treecff2b8a73606da98e98cd74e0ccabc70b281a847 /lisp
parent0bd6ae773a1ade1bdec2c233df4f260d028fd6c5 (diff)
downloademacs-fab23328512e47a50caced8d074e86e583cc8a9f.tar.gz
cc-mode: add ‘c-lineup-ternary-bodies’ (bug#41061)
Introduce ‘c-lineup-ternary-bodies’ function which, when used as a c lineup function, aligns question mark and colon of a ternary operator. For example: return arg % 2 == 0 ? arg / 2 : (3 * arg + 1); * lisp/progmodes/cc-align.el (c-lineup-ternary-bodies): New function. * doc/misc/cc-mode.texi (Operator Line-Up Functions): Document the new function. * test/lisp/progmodes/cc-mode-tests.el (c-lineup-ternary-bodies): New test case.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/progmodes/cc-align.el32
1 files changed, 32 insertions, 0 deletions
diff --git a/lisp/progmodes/cc-align.el b/lisp/progmodes/cc-align.el
index f30477dc787..c49bdc5c518 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 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 (c-syntactic-skip-backward "^?:" limit t)
+ (goto-char (1- (point)))
+ (cond ((eq (char-after) ??)
+ ;; If we’ve found a question mark, decrease depth. If we’re
+ ;; 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