1

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'?

0

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)
  • Thanks this worked, after I removed the '#' from the add-hook part. I haven't done much elisp. Is that a typo or something that I'm missing the point of? – Mitchk Jun 18 at 23:58
  • @Mitchk The sharp quote #' 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 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.