我在C++中有一个很长的函数声明,我正在emacs中编写。带括号的缩进行为对80列没有例外,看起来像:
std::vector<std::vector<double> > doFooBarBlahBlah(const std::map<std::pair<unsigned, std::string>, FoobarType> fooArg1,
const std::map<std::pair<unsigned, std::string>, FoobarType> fooArg2) {
将参数移动到下一行并自动缩进结果为:
std::vector<std::vector<double> > doFooBarBlahBlah(
const std::map<std::pair<unsigned, std::string>, FoobarType> fooArg1,
const std::map<std::pair<unsigned, std::string>, FoobarType> fooArg2) {
谷歌C++风格指南建议:
std::vector<std::vector<double> > doFooBarBlahBlah(
const std::map<std::pair<unsigned, std::string>, FoobarType> fooArg1,
const std::map<std::pair<unsigned, std::string>, FoobarType> fooArg2) {
是否有一个emacs扩展可以以尊重此规则的方式自动缩进?
编辑以说明列长度异常这将为你做的把戏:
(defun my-c-custom-settings ()
(c-set-offset 'arglist-intro 'my-special-indent))
(add-hook 'c-mode-common-hook 'my-c-custom-settings)
(defun my-special-indent (pair)
(let* ((symbol (car pair))
(offset (cdr pair))
(regular-column (c-lineup-arglist-intro-after-paren symbol)))
(if (> (save-excursion (+ (aref regular-column 0)
(- (progn (end-of-line) (current-column))
(progn (beginning-of-line)
(skip-chars-forward " t")
(current-column)))))
80)
'+
regular-column)))
找出需要为缩进设置什么设置的方法是将光标移动到要以不同方式缩进的点,然后执行:
M-x c-set-offset
又名C-C-o。在这种情况下,您希望将其设置为'+
,表示要比当前级别多缩进一个级别。其中一个设置可以是返回偏移量的函数。
手册中有大量关于缩进的cc模式的信息,包括如何自定义(我在上面的示例中采用了简单的方法)。以及c-offsets-list的文档。