一位同事为我提供了一个我们正在研究的C ++项目的clang格式样式文件。我安装了clang-format.el,以便能够从emacs格式化缓冲区。重新格式化按预期工作。但是,Emacs默认的c-mode缩进仍然完全不同。
我发现在编辑和销毁以后恢复源代码格式时会非常令人不安。有没有办法读取clang格式文件并应用相应的cc模式选项?
答案 0 :(得分:2)
我不知道是否有任何直接转换工具。但是,您可以尝试使用以下技巧:
将项目中相当数量的C ++文件连接成单个文件(例如cat *.cpp > single.cpp
)
将clang格式应用于single.cpp
在Emacs中打开single.cpp
使用CC模式的guess功能:M-x c-guess-no-install
然后M-x c-guess-view
答案 1 :(得分:0)
我参加聚会有点晚了,但希望有人会发现这仍然有用。我在下面发布了我的clang-format配置的相关部分,该部分提供了您想要的功能。
(use-package clang-format
:after (s)
:init
(defun get-clang-format-option (config-str field is-num)
"Retrieve a config option from a clang-format config.
CONFIG-STR is a string containing the entire clang-format config.
FIELD is specific option, e.g. `IndentWidth'. IS-NUM is a
boolean that should be set to 1 if the option is numeric,
otherwise assumed alphabetic."
(if is-num
(let ((primary-match (s-match (concat "^" field ":[ \t]*[0-9]+") config-str)))
(if primary-match
(string-to-number (car (s-match "[0-9]+" (car primary-match))))
0))
(let ((primary-match (s-match (concat "^" field ":[ \t]*[A-Za-z]+") config-str)))
(if primary-match
(car (s-match "[A-Za-z]+$" (car primary-match)))
""))))
:hook (c-mode-common . (lambda ()
(let* ((clang-format-config
(shell-command-to-string "clang-format -dump-config"))
(c-offset (get-clang-format-option clang-format-config "IndentWidth" t))
(tabs-str (get-clang-format-option clang-format-config "UseTab" nil))
(base-style
(get-clang-format-option clang-format-config "BasedOnStyle" nil)))
(progn
(if (> c-offset 0)
(setq-local c-basic-offset c-offset)
(if (not (equal "" base-style))
(cond ((or (equal "LLVM" base-style)
(equal "Google" base-style)
(equal "Chromium" base-style)
(equal "Mozilla" base-style))
(setq-local c-basic-offset 2))
((equal "WebKit" base-style)
(setq-local c-basic-offset 4)))))
(if (not (equal "" tabs-str))
(if (not (string-equal "Never" tabs-str))
(setq-local indent-tabs-mode t)
(setq-local indent-tabs-mode nil))
(if (not (equal "" base-style))
(cond ((or (equal "LLVM" base-style)
(equal "Google" base-style)
(equal "Chromium" base-style)
(equal "Mozilla" base-style)
(equal "WebKit" base-style))
(setq-local indent-tabs-mode nil))))))))))
我使用use-package。否则,您将不得不进行一些小的调整。另外,请注意,我使用s.el进行字符串操作。
代码查找“ IndentWidth”和“ UseTab”。如果找到“ UseTab”并且未将其设置为“ Never”,则将indent-tabs-mode
设置为t
。否则,我将其设置为nil
。 “ IndentWidth”的值将转到c-basic-offset
。如果找不到这些字段,但是找到“ BasedOnStyle”,则根据该样式设置适当的值。我没有包含Microsoft样式,因为我的clang-format版本不会为我提供配置转储(大概是在更高版本中)。 “ IndentWidth”和“ UseTab”将覆盖“ BasedOnStyle”的行为,该行为与clang格式的行为一致。最后,请注意,c-basic-offset
和indent-tabs-mode
被设置为缓冲区局部变量,因此在处理具有不同配置的多个文件时,此设置可以按预期工作。