在emacs中控制代码美化

发布于 2024-09-05 07:15:59 字数 44 浏览 1 评论 0原文

如何让 Emacs 在左括号周围添加填充空格并在缩进 () 上添加运算符?

How can I get Emacs to put padding spaces around opening brackets and operators on indentation ()?

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

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

发布评论

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

评论(2

携君以终年 2024-09-12 07:15:59

正如其他答案中所述,有解决方案可以解决您的问题。但是,代码美化并不总是可以作为一种选择,因为您可能正在使用另一个编码标准处理另一个项目。在为项目做出贡献时,您最不想做的事情就是在提交补丁之前弄乱代码的风格,原因如下:

  1. 您将提交一个大的 DIFF,它将由您已修复的错误加上您的美化组成努力。许多项目都有政策,其中对代码的唯一更改应该是修复(不更改样式),除非努力更改样式。
  2. 如果您想提交一个干净的差异(仅包含错误修复),您将必须返回代码以消除您的风格差异。

幸运的是,有一个中途之家可以让您和维护人员保持理智,glasses-mode

眼镜次要模式(指示器 o^o):
用于制作标识符的次要模式
像这样可读。当该模式为
活动,它尝试添加虚拟
分隔符(如下划线)位于
他们所属的地方。

它不仅会使标识符更具可读性,还会在函数括号之前放置一个空格。 glasses-mode 只是“假装”代码美丽,仅供您的眼睛使用。至少值得注意。

As stated in other answers there are solutions to solve your problem. But, code beautifying is not always available as an option as you may be working on another project with another coding standard. The last thing you want to be doing when contributing to a project is messing with the style of the code before you submit your patch for these reasons:

  1. You will submit a large DIFF which will be made up of the bugs you have fixed PLUS your beautification efforts. Many projects have policy where the only change to the code should be fixes (no style changes) unless there is an effort to change the style.
  2. If you want to submit a clean diff (with just your bug fixes) you would have to go back through the code undoing your style difference.

Luckily, there is a half-way-house which will keep you and maintainers sane, glasses-mode:

Glasses minor mode (indicator o^o):
Minor mode for making identifiers
likeThis readable. When this mode is
active, it tries to add virtual
separators (like underscores) at
places they belong to.

Not only will it make identifiers more readable, it will also place a space before your function brackets. glasses-mode just 'pretends' that the code is beautiful, for your eyes only. Note-worthy at the very least.

风尘浪孓 2024-09-12 07:15:59

您可以建议 indent-region 函数在缩进区域后应用填充,如下所示:

(defadvice indent-region (after pad-brackets-and-operators activate)
  (save-excursion
    (save-restriction
      (narrow-to-region (point) (mark))
      (goto-char (point-min))
      (while (re-search-forward " *\\([()+-*/]\\) *")
        (replace-match " \\1 ")
        (backward-char 1)))))

You could advise the indent-region function to apply the padding after indenting the region, like so:

(defadvice indent-region (after pad-brackets-and-operators activate)
  (save-excursion
    (save-restriction
      (narrow-to-region (point) (mark))
      (goto-char (point-min))
      (while (re-search-forward " *\\([()+-*/]\\) *")
        (replace-match " \\1 ")
        (backward-char 1)))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文