A code base I work on has the macro
#define when break; case
to make switch statements 'cleaner'
However this causes problems with indentation,
Is it possible to set the behavior of 'when' to be the same as 'case'?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityA code base I work on has the macro
#define when break; case
to make switch statements 'cleaner'
However this causes problems with indentation,
Is it possible to set the behavior of 'when' to be the same as 'case'?
The promoted way would be to add a new cc-mode
with c-add-language
(see the example ) linked in cc-langs.el
. But that is a bit heavy for the task at hand.
It is simpler to add when
as a font lock keyword and as a matcher in c-labels-kwds-regexp
.
You can use the following c-mode-hook
:
(defun my-c-setup ()
"Run in `c-mode-hook'."
(font-lock-add-keywords nil '(("when" . 'font-lock-keyword-face)))
(setq c-label-kwds-regexp (replace-regexp-in-string "case" "case\\\\|when" c-label-kwds-regexp)))
(add-hook 'c-mode-hook #'my-c-setup)
#'
stands for wrapping the quoted form with (function ...)
. You should use it everywhere you could syntactically replace the function symbol (here my-c-setup
) with a lambda. See the item about sharp quotes in Section "Functions" of the Elisp style guide and also the corresponding discussion. There is also a nice article from Malabarba about that.
– Tobias
Jun 19 '19 at 1:54