在 Emacs 中,如何在一系列初始化语句中排列等号?

发布于 2024-07-21 11:08:38 字数 366 浏览 9 评论 0原文

我在某处看到过这个,但现在找不到了。 emacs 中是否有内置函数,或者有人有 elisp,可以在 cc 模式下将一系列初始化语句中的所有等号排列起来?

之前:

int t=9;
Graphics g = new Graphics();
List<String> list = new List<String>();

之后:

int          t    = 9;
Graphics     g    = new Graphics();
List<String> list = new List<String>();

I saw this somewhere, but cannot find it now.
Is there a built-in function in emacs, or does someone have elisp, to line up all the equals signs in a series of inititialization statments in cc-mode?

Before:

int t=9;
Graphics g = new Graphics();
List<String> list = new List<String>();

After:

int          t    = 9;
Graphics     g    = new Graphics();
List<String> list = new List<String>();

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

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

发布评论

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

评论(3

澉约 2024-07-28 11:08:38

使用Mxalign-regexp(此处为Mxalign-regexp RET = RET)。 您还可以向变量 align-rules-list 添加“对齐规则”,以便将来 Mxalign 执行此操作。 有关详细信息,请参阅文档 (Ch falign)。

Use M-x align-regexp (here, M-x align-regexp RET = RET). You can also add an "alignment rule" to the variable align-rules-list, so that in future M-x align will do it. See the documentation (C-h f align) for details.

最冷一天 2024-07-28 11:08:38

这是对 harpo 对 ShreevatsaR 答案的评论的回应:

唯一的问题是它“制表”输出,这可能不是您想要的。 你知道有什么办法可以防止这种情况吗?

以下是我为解决该问题所做的操作:

;; Align with spaces only
(defadvice align-regexp (around align-regexp-with-spaces)
  "Never use tabs for alignment."
  (let ((indent-tabs-mode nil))
    ad-do-it))
(ad-activate 'align-regexp)

This is in response to harpo's comment to ShreevatsaR's answer:

The only problem with this is that it "tabifies" the output, which is probably not what you want. Do you know any way to prevent this?

Here's what I did to resolve that issue:

;; Align with spaces only
(defadvice align-regexp (around align-regexp-with-spaces)
  "Never use tabs for alignment."
  (let ((indent-tabs-mode nil))
    ad-do-it))
(ad-activate 'align-regexp)
月依秋水 2024-07-28 11:08:38

Mx 对齐应该可以解决问题。

M-x align should do the trick.

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