关于在cc-mode中,enum class的缩进的问题

今天在使用spacemacs写c++中的 enum class时发现一个缩进的问题:

enum class kind:char{
    //expressions
        };

我需要的是

enum class kind:char{
    //expressions
};

在stackoverflow上查了一下,找到了如下一段elisp代码:

(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)

翻了一下底下的issue,修改了一下里面用到的正则,但是还是不能运行,求教一下具体是哪里有问题?应该怎么改?

先谢谢各位解答的大神。

c++-mode 不存在这个问题,即便是在零配置的情况下,你可以跑一下这份测试:

;;; Usage: /path/to/emacs -nw -Q -l /path/to/test-indent-of-cc-mode.el
;;; Date: 2017-09-29_11.07.36
(toggle-debug-on-error)

(add-hook 'after-init-hook
          (lambda ()
            ;; 这两行并非必需,可以删掉
            (setq c-default-style "k&r")
            (setq c-basic-offset 4)

            ;; 缩进测试
            (switch-to-buffer "*.cc")
            (c++-mode)
            (insert "\
enum class Colors {
Red = 1,
Blue,
Green
};
")
            (mark-whole-buffer)
            (indent-for-tab-command)
            ))

(run-hooks 'after-init-hook)
;;; test-indent-of-cc-mode.el ends here

建议你也做一份基于 spacemacs 的最小配置,确定是谁的锅。

1 个赞