简体   繁体   English

如何将emacs C ++模式中的代码与“;”或“,”对齐?

[英]How to align codes in emacs C++ mode to “;” or “,”?

As a test engineer, I often have some spaghetti code like below: 作为测试工程师,我经常有一些意大利面条代码,如下所示:

  int *const cpe = &n; assert(42 == *cpe);
  int *const cpf = &cn; assert(42 == *cpf);
  int *const cpg = pcn; assert(42 == *cpg);
  int *const cph = cpcn; assert(42 == *cph);

For aesthetics, I would like to align them in columns defined by " ; ", like below: 对于美学,我想将它们对齐在由“ ; ”定义的列中,如下所示:

  int *const cpe = &n;   assert(42 == *cpe);
  int *const cpf = &cn;  assert(42 == *cpf);
  int *const cpg = pcn;  assert(42 == *cpg);
  int *const cph = cpcn; assert(42 == *cph);

Is there a way in emacs to do this? 有没有办法在emacs中这样做? (I know Mx align but it is not doing a neat job like desired.) Hopefully the method should also work with "," too. (我知道Mx对齐,但它并没有像所希望的那样做一个整洁的工作。)希望这个方法也应该与“,”一起工作。

(add-to-list 'align-rules-list
             '(c-assignment1
               (regexp . "[=;]\\(\\s-*\\)")
               (mode   . '(c-mode))
               (repeat . t)))

Simply Mx align works too, if you write this code. 如果您编写此代码,简单地Mx align也可以。

In this particular case, you can just align on assert while the region is active: 在这种特殊情况下,您可以在区域处于活动状态时对assert进行对齐:

M-x align-regexp assert RET

In general, the answer here is similar to the first part of my answer to your other question : 一般来说,这里的答案类似于我对你的另一个问题的答案的第一部分:

C-u M-x align-regexp RET ;\(\s-*\) RET RET RET n

will turn 将转向

  int *const cpe = &n; assert(42 == *cpe);
  int *const cpf = &cn; assert(42 == *cpf);
  int *const cpg = pcn; assert(42 == *cpg);
  int *const cph = cpcn; assert(42 == *cph);

into

  int *const cpe = &n;   assert(42 == *cpe);
  int *const cpf = &cn;  assert(42 == *cpf);
  int *const cpg = pcn;  assert(42 == *cpg);
  int *const cph = cpcn; assert(42 == *cph);

The same technique can be used to align on commas. 可以使用相同的技术来对齐逗号。 Just replace the ; 只需更换; in the regular expression with , . 在正则表达式中,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM