Created
May 7, 2012 06:44
-
-
Save nschum/2626303 to your computer and use it in GitHub Desktop.
better "enum class" indent in Emacs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; This hack fixes indentation for C++11's "enum class" in Emacs. | |
;; http://stackoverflow.com/questions/6497374/emacs-cc-mode-indentation-problem-with-c0x-enum-class/6550361#6550361 | |
(defun inside-class-enum-p (pos) | |
"Checks if POS is within the braces of a C++ \"enum class\"." | |
(ignore-errors | |
(save-excursion | |
(goto-char pos) | |
(up-list -1) | |
(backward-sexp 1) | |
(looking-back "enum[ \t]+class[ \t]+[^}]+")))) | |
(defun align-enum-class (langelem) | |
(if (inside-class-enum-p (c-langelem-pos langelem)) | |
0 | |
(c-lineup-topmost-intro-cont langelem))) | |
(defun align-enum-class-closing-brace (langelem) | |
(if (inside-class-enum-p (c-langelem-pos langelem)) | |
'- | |
'+)) | |
(defun fix-enum-class () | |
"Setup `c++-mode' to better handle \"class enum\"." | |
(add-to-list 'c-offsets-alist '(topmost-intro-cont . align-enum-class)) | |
(add-to-list 'c-offsets-alist | |
'(statement-cont . align-enum-class-closing-brace))) | |
(add-hook 'c++-mode-hook 'fix-enum-class) |
For me
(or (looking-back "enum\\s-+class\\s-+")
(looking-back "enum\\s-+class\\s-+\\S-+\\s-*:\\s-*"))
worked
even with the solution from hhyyrylainen the closing brace has an extra space in front of it for some reason
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tweaked the regexp to handle the enum-base case, e.g.
enum class Foo : uint8_t {
Only missing thing is looking-back doesn' t do newlines.
That's beyond my elisp fu :(