emacs:类和结构体的不同缩进 [英] emacs: different indentation for class and struct

查看:164
本文介绍了emacs:类和结构体的不同缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在emacs中实现以下缩进:

  class A 
{
//我一直使用类中的访问标签

public:// access-label
int member; // inclass
};

struct B
{
//我从不在结构体中使用访问标签

int member; // inclass
};

但是使用以下配置文件...




(c ++ - mode)
(c-set-stylecode $ c $
(setq c-basic-offset 4)
(c-set-offset'access-label' - )
(c-set-offset'inclass'++ )
;; ...
(setq mode-nameMy C ++)

(add-to-list'auto-mode-alist'(\ \。[ch] p?p?\\'。my-cpp-mode))

...仅实现:

  class A 
{
public:// access -label
int member; // inclass
};

struct B
{
//此缩进太长
int member; // inclass
};

当然这是因为:



    $对于缩进,b $ b
  • class和struct之间显然没有区别(这是inclass),

  • inclass取决于访问标签的存在。任何想法,我可以如何使indlass的东西依赖于类/结构体或存在访问标签的缩进?

p>

解决方案

新答案



您在问题中提到的确切要求。
我不得不根据我的新项目的编码风格设置缩进。经过一番研究,我使用了 Custom Line-更改功能



将您的 my-cpp-mode 修改为:

 (defun my-c-lineup-inclass(langelem)
(let((inclass (assoc'inclass c-syntax-context)))
(save-excursion
(goto-char(c-langelem-pos inclass))
(if(或(looking-结构)
(查找typedef结构))
'+
'++))))

(defun my-cpp-mode )
我的C ++模式
(c ++ - 模式)
(c-set-styleK& R)
(setq c-basic-offset 4)
(c-set-offset'access-label' - )
(c-set-offset'inclass'my-c-lineup-inclass)
;; ...
setq mode-nameMy C ++)

如果这个答案可以接受,我我会继续删除旧的答案。 p>

旧答案



根据您要实现的目标,我可以建议不同的做法?您似乎希望访问标签与类和类成员不同的缩进级别。使用以下内容来实现。

 (access-label。/)

从Emacs文档:


如果OFFSET是 +'之一, - ', ++', - ', *'或 /'
然后将c-basic-offset的正或负倍数添加到
基础缩进; 1,-1,2,-2,0.5和-0.5。


这是一个自定义的片段我已经定义的样式。

 (c-add-style 
xyz-
(c-base-offset。)
(c-offsetets- (
(access-label。/)
(inextern-lang。0)
(innamespace。0)
(member-init-intro。++)
))))

使用 c-basic-offset 设置为4,(access-label。/)向访问标签添加了2个空格的负缩进。以下是您的示例代码的缩进模式的实际结果。

  class A 
{
//我总是在类中使用访问标签

public:// access-label
int member; // inclass
};

struct B
{
//我从不在结构体中使用访问标签

int member; // inclass
};

我推荐这种模式,因为成员变量/结构成员的缩进级别是一致的。 FWIW, Google C Style 遵循相同的方法。 / p>

据我所知,无法区分类成员或结构成员( inclass sytax元素) 。您可以使用 M-x c-syntax-on-region 在区域上进行句法分析。对您的一个例子的分析结果如下。从输出中,如果你在一个类或一个结构体中,没有任何区别。

  class A //((topmost-intro 1))
{//((class-open 1))
//((inclass 64)(topmost-intro 64)(comment-intro) )我一直使用类
//((inclass 64)(topmost-intro 64))中的访问标签
public://((inclass 64)(access-label 64))access-label
int成员; //((inclass 64)(topmost-intro 64))inclass
}; //((class-close 1))
//((topmost-intro 503))
struct B //((topmost-intro 503))
{//((class -open 629))
//((inclass 694)(topmost-intro 694)(comment-intro))我从不在结构体中使用访问标签
//((inclass 694)(topmost-intro 694))
int member; //((inclass 694)(topmost-intro 694))inclass
}; //((class-close 629))


I'm trying to achieve the following indentation in emacs:

class A
{
    // I ALWAYS use access labels in classes

    public: // access-label
        int member; // inclass
};

struct B
{
    // I NEVER use access labels in structs

    int member; // inclass
};

However with the following configuration file...

(defun my-cpp-mode ()
  "My C++ mode"
  (c++-mode)
  (c-set-style "K&R")
  (setq c-basic-offset 4)
  (c-set-offset 'access-label '-)
  (c-set-offset 'inclass '++)
  ;; ...
  (setq mode-name "My C++")
)
(add-to-list 'auto-mode-alist '("\\.[ch]p?p?\\'" . my-cpp-mode))

... I achieve only:

class A
{
    public: // access-label
        int member; // inclass
};

struct B
{
        // this indentation is too long
        int member; // inclass
};

Of course that's because:

  • for the indentation there is obviously no difference between "class" and "struct" (it's all "inclass"),
  • the indentation of "inclass" stuff doesn't depend on the presence of access labels or not.

Any idea how I can make the indentation of inclass stuff dependent on either class/struct or on the presence of access labels?

解决方案

New Answer

I ran into the exact requirement that you had mentioned in your question. I had to setup indentation according to the coding style of my new project. After a bit of research, I achieved this using Custom Line-up Functions.

Modify your my-cpp-mode to look like this:

(defun my-c-lineup-inclass (langelem)
  (let ((inclass (assoc 'inclass c-syntactic-context)))
    (save-excursion
      (goto-char (c-langelem-pos inclass))
      (if (or (looking-at "struct")
              (looking-at "typedef struct"))
          '+
        '++))))

(defun my-cpp-mode ()
  "My C++ mode"
  (c++-mode)
  (c-set-style "K&R")
  (setq c-basic-offset 4)
  (c-set-offset 'access-label '-)
  (c-set-offset 'inclass 'my-c-lineup-inclass)
  ;; ...
  (setq mode-name "My C++")
)

If this answer is acceptable, I'll go ahead and remove the old answer.

Old Answer

Based on what you are trying to achieve, may I suggest a different approach? You seem to want the access label at a different indentation level than the class and the class members. Use the following to achieve that.

(access-label . /)

From Emacs documentation:

If OFFSET is one of the symbols +',-', ++',--', *', or/' then a positive or negative multiple of `c-basic-offset' is added to the base indentation; 1, -1, 2, -2, 0.5, and -0.5, respectively.

Here is a snippet from one of the custom styles that I have defined.

(c-add-style
 "xyz-style"
 '((indent-tabs-mode . nil)
   (fill-column . 75)
   (c-basic-offset . 4)
   (c-offsets-alist . (
                       (access-label . /)
                       (inextern-lang . 0)
                       (innamespace . 0)
                       (member-init-intro . ++)
                       ))))

With c-basic-offset set to 4, (access-label . /) adds a negative indentation of 2 spaces to the access labels. Here is the actual result of my indentation mode on your sample code.

class A
{
    // I ALWAYS use access labels in classes

  public: // access-label
    int member; // inclass
};

struct B
{
    // I NEVER use access labels in structs

    int member; // inclass
};

I recommend this mode because, the indentation level of the member variables/struct members is consistent. FWIW, Google C Style follows the same approach.

As far as I can tell, one cannot differentiate between a class member or a struct member (inclass sytax element). You can use M-x c-syntactic-information-on-region to do a syntactic analysis on a region. One such analysis on you example yields the following. From the output, there is nothing to differentiate between if you are in a class or a struct.

class A                                 // ((topmost-intro 1))
{                                       // ((class-open 1))
                                        // ((inclass 64) (topmost-intro 64) (comment-intro))I ALWAYS use access labels in classes
                                        // ((inclass 64) (topmost-intro 64))
  public:                               // ((inclass 64) (access-label 64))access-label
    int member;                         // ((inclass 64) (topmost-intro 64))inclass
};                                      // ((class-close 1))
                                        // ((topmost-intro 503))
struct B                                // ((topmost-intro 503))
{                                       // ((class-open 629))
                                        // ((inclass 694) (topmost-intro 694) (comment-intro))I NEVER use access labels in structs
                                        // ((inclass 694) (topmost-intro 694))
    int member;                         // ((inclass 694) (topmost-intro 694))inclass
};                                      // ((class-close 629))

这篇关于emacs:类和结构体的不同缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆