C++ emacs 中的样式:默认括号自动缩进行为的大于 80 列例外

发布于 2025-01-06 04:53:37 字数 1191 浏览 3 评论 0原文

我有一个很长的 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) {

Moving the argument to the next line and auto-indenting results in:

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

The google C++ style Guide suggest:

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

Is There an emacs extension toautomatic indentation in a way尊重这条规则?

I have a long function declaration in C++ that I'm writing up in emacs. The indentation behavior with parenthesis doesn't make an exception for 80 columns and looks like:

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

Moving the argument to the next line and auto-indenting results in:

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

The google C++ style guide suggests:

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

Is there an emacs extension to automate indentation in a way that respects this rule?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰つ倒转 2025-01-13 04:53:37

编辑以考虑列长度异常
这将为您解决问题:

(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

又名 Cc Co。在本例中,您希望将其设置为 '+ 表示比当前级别多缩进一级。设置之一可以是返回偏移量的函数。

缩进,包括如何自定义它(我在上面的示例中采用了简单的方法)。以及 c-offsets-alist 的文档。

Edited to account for column length exception
This will do the trick for you:

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

The way to find out what setting needs be set for indentation is to move your cursor to the point you want to indent differently and do:

M-x c-set-offset

aka C-c C-o. And in this case you want to set it to '+ indicating to indent one more level than the current level. One of the settings can be a function which returns the offset.

There's a ton of information available in the manual for cc-mode on indentation, including how to customize it (I took the easy way in the sample above). As well as the documentation for c-offsets-alist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文