Skip to content

Instantly share code, notes, and snippets.

@musteresel
Forked from nschum/gist:2626303
Last active October 24, 2016 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musteresel/c0a631a6bdd80c755c895c894c68e0a9 to your computer and use it in GitHub Desktop.
Save musteresel/c0a631a6bdd80c755c895c894c68e0a9 to your computer and use it in GitHub Desktop.
better "enum class" indent in Emacs
;;; enum-class-indent.el --- Fixing enum class indentation -*- lexical-binding: t; -*-
;; Keywords: c++
;; Version: 0.0.1
;;; Commentary:
;; 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
;;; Code:
(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)
(provide 'enum-class-indent)
;;; enum-class-indent.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment