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

mod*_*ler 3 c++ emacs

作为一名测试工程师,我经常有一些像下面这样的意大利面条式代码:

  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);
Run Code Online (Sandbox Code Playgroud)

为了美观,我想将它们在由“ ; ”定义的列中对齐,如下所示:

  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);
Run Code Online (Sandbox Code Playgroud)

emacs 中有没有办法做到这一点?(我知道,M-x align但它并没有像期望的那样做一个整洁的工作。)希望该方法也应该与“,”一起使用。

Shi*_*ama 5

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

M-x align如果您编写此代码,也很简单。