aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichal Nazarewicz2020-05-03 16:32:47 +0100
committerMichal Nazarewicz2020-05-09 11:30:32 +0100
commitfab23328512e47a50caced8d074e86e583cc8a9f (patch)
treecff2b8a73606da98e98cd74e0ccabc70b281a847
parent0bd6ae773a1ade1bdec2c233df4f260d028fd6c5 (diff)
downloademacs-fab23328512e47a50caced8d074e86e583cc8a9f.tar.gz
emacs-fab23328512e47a50caced8d074e86e583cc8a9f.zip
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.
-rw-r--r--doc/misc/cc-mode.texi20
-rw-r--r--etc/NEWS17
-rw-r--r--lisp/progmodes/cc-align.el32
-rw-r--r--test/lisp/progmodes/cc-mode-tests.el21
4 files changed, 90 insertions, 0 deletions
diff --git a/doc/misc/cc-mode.texi b/doc/misc/cc-mode.texi
index f9c9f5e1830..16eac4828c7 100644
--- a/doc/misc/cc-mode.texi
+++ b/doc/misc/cc-mode.texi
@@ -6395,6 +6395,26 @@ function is the same as specifying a list @code{(c-lineup-assignments
6395 6395
6396@comment ------------------------------------------------------------ 6396@comment ------------------------------------------------------------
6397 6397
6398@defun c-lineup-ternary-bodies
6399@findex lineup-ternary-bodies @r{(c-)}
6400Line up true and false branches of a ternary operator
6401(i.e. @code{?:}). More precisely, if the line starts with a colon
6402which is a part of a said operator it with corresponding question
6403mark. For example:
6404
6405@example
6406@group
6407return arg % 2 == 0 ? arg / 2
6408 : (3 * arg + 1); @hereFn{c-lineup-ternary-bodies}
6409@end group
6410@end example
6411
6412@workswith @code{arglist-cont}, @code{arglist-cont-nonempty} and
6413@code{statement-cont}.
6414@end defun
6415
6416@comment ------------------------------------------------------------
6417
6398@defun c-lineup-cascaded-calls 6418@defun c-lineup-cascaded-calls
6399@findex lineup-cascaded-calls @r{(c-)} 6419@findex lineup-cascaded-calls @r{(c-)}
6400Line up ``cascaded calls'' under each other. If the line begins with 6420Line up ``cascaded calls'' under each other. If the line begins with
diff --git a/etc/NEWS b/etc/NEWS
index 9c71752b621..12406eea822 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -305,6 +305,23 @@ use ‘doxygen’ by default one might evaluate:
305 305
306or use it in a custom ‘c-style’. 306or use it in a custom ‘c-style’.
307 307
308*** Added support to line up ‘?’ and ‘:’ of a ternary operator.
309The new ‘c-lineup-ternary-bodies’ function can be used as a lineup
310function to align question mark and colon which are part of a ternary
311operator (‘?:’). For example:
312
313 return arg % 2 == 0 ? arg / 2
314 : (3 * arg + 1);
315
316To enable, add it to appropriate entries in ‘c-offsets-alist’, e.g.:
317
318 (c-set-offset 'arglist-cont '(c-lineup-ternary-bodies
319 c-lineup-gcc-asm-reg))
320 (c-set-offset 'arglist-cont-nonempty '(c-lineup-ternary-bodies
321 c-lineup-gcc-asm-reg
322 c-lineup-arglist))
323 (c-set-offset 'statement-cont '(c-lineup-ternary-bodies +))
324
308** browse-url 325** browse-url
309 326
310*** Added support for custom URL handlers 327*** Added support for custom URL handlers
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."
790 (or (c-lineup-assignments langelem) 790 (or (c-lineup-assignments langelem)
791 c-basic-offset)) 791 c-basic-offset))
792 792
793(defun c-lineup-ternary-bodies (langelem)
794 "Line up true and false branches of a ternary operator (i.e. ‘?:’).
795More precisely, if the line starts with a colon which is a part of
796a said operator it with corresponding question mark; otherwise return
797nil. For example:
798
799 return arg % 2 == 0 ? arg / 2
800 : (3 * arg + 1); <- c-lineup-ternary-bodies
801
802Works with: arglist-cont, arglist-cont-nonempty and statement-cont.
803"
804 (save-excursion
805 (back-to-indentation)
806 (when (and (eq ?: (char-after))
807 (not (eq ?: (char-after (1+ (point))))))
808 (let ((limit (c-langelem-pos langelem)) (depth 1))
809 (catch 'done
810 (while (c-syntactic-skip-backward "^?:" limit t)
811 (goto-char (1- (point)))
812 (cond ((eq (char-after) ??)
813 ;; If we’ve found a question mark, decrease depth. If we’re
814 ;; reached zero, we’ve found the one we were looking for.
815 (when (zerop (setq depth (1- depth)))
816 (throw 'done (vector (current-column)))))
817 ((or (eq ?: (char-before)) (eq ?? (char-before)))
818 ;; Step over ‘::’ and ‘?:’ operators. We don’t have to
819 ;; handle ‘?:’ here but doing so saves an iteration.
820 (if (eq (point) limit)
821 (throw 'done nil)
822 (goto-char (1- (point)))))
823 ((setq depth (1+ depth)))))))))) ; Otherwise increase depth.
824
793(defun c-lineup-cascaded-calls (langelem) 825(defun c-lineup-cascaded-calls (langelem)
794 "Line up \"cascaded calls\" under each other. 826 "Line up \"cascaded calls\" under each other.
795If the line begins with \"->\" or \".\" and the preceding line ends 827If the line begins with \"->\" or \".\" and the preceding line ends
diff --git a/test/lisp/progmodes/cc-mode-tests.el b/test/lisp/progmodes/cc-mode-tests.el
index 0729841ce6f..ad7a52b40d9 100644
--- a/test/lisp/progmodes/cc-mode-tests.el
+++ b/test/lisp/progmodes/cc-mode-tests.el
@@ -78,4 +78,25 @@
78 (insert macro-string) 78 (insert macro-string)
79 (c-mode)))) 79 (c-mode))))
80 80
81(ert-deftest c-lineup-ternary-bodies ()
82 "Test for c-lineup-ternary-bodies function"
83 (with-temp-buffer
84 (c-mode)
85 (let* ((common-prefix "int value = condition ")
86 (expected-column (length common-prefix)))
87 (dolist (test '(("? a : \n b" . nil)
88 ("? a \n ::b" . nil)
89 ("a \n : b" . nil)
90 ("? a \n : b" . t)
91 ("? ::a \n : b" . t)
92 ("? (p ? q : r) \n : b" . t)
93 ("? p ?: q \n : b" . t)
94 ("? p ? : q \n : b" . t)
95 ("? p ? q : r \n : b" . t)))
96 (delete-region (point-min) (point-max))
97 (insert common-prefix (car test))
98 (should (equal
99 (and (cdr test) (vector expected-column))
100 (c-lineup-ternary-bodies '(statement-cont . 1))))))))
101
81;;; cc-mode-tests.el ends here 102;;; cc-mode-tests.el ends here