使 Emacs ESS 遵循 R 风格指南
我已经使用 Emacs/ESS 有一段时间了,并且我熟悉 Hadley 的 R 风格建议。我想遵循 ESS 中的这些约定,例如运算符周围的那些漂亮空格、逗号后面和 if
语句之后、大括号之前的空格等。
有没有人愿意遵循这个风格指南?恕我直言,官方的风格推荐相当谦虚,他们对风格只字未提。 Google R 风格指南 与我的指南太相似当我用 JavaScript 编码时使用,所以这是一个禁忌。
长话短说:是否有任何具有 (e)LISP 技能的人愿意为 ESS 实施(Hadley 的)风格指南?
I've been using Emacs/ESS for quite a while, and I'm familiar with Hadley's R style recommendations. I'd like to follow these conventions in ESS, like those nice spaces around operators, space after comma and after if
statement, before curly braces, etc.
Did anyone even bothered to follow this style guide at all? IMHO, official style recommendations are quite modest, and they say nothing about the style whatsoever. Google R style guide are too similar with the ones I use when I code in JavaScript, so it's a no-no.
Long story short: is there anyone with (e)LISP skills willing to implement (Hadley's) style guide for ESS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不写 Elisp,而且我不同意 Hadley 关于下划线风格优点的观点。此外,Hadley 仍然迷失在不使用 OneTrueEditor 的沙漠中,所以我们不能指望他在这个问题上提供任何帮助。
但如果您愿意关注 R Core 而不是 Hadley,下面是 R 内部手册,第 8 节“R 编码标准” 建议。对我来说,R Core 首先定义了 R 风格。 Google 和 Hadley 的风格是很好的次要推荐。
无论如何,回到 Elisp。以下内容多年来一直为我们提供了良好的服务,而且我确实喜欢这样一个事实:基本的 R 行为与 Emacs C++ 风格类似,因为我碰巧经常查看这两种模式下的代码。
我认为我经常做的唯一添加是遵循最后一个注释掉的片段:
如果您确实需要使用下划线进行编码,您当然可以关闭下划线切换。
I don't write Elisp, and I disagree with Hadley about the stylistic merits of underscores. Moreover, Hadley is still lost in the desert of not using the OneTrueEditor so we can expect no help from him on this on this issue.
But if you are open to follow R Core rather than Hadley, below is what the R Internals manual, section 8. "R Coding Standards" recommends. To me, it is R Core who defines R style first and foremost. Google's and Hadley's styles are nice secondary recommendations.
Anyway, back to Elisp. The following has served we well for many years, and I do like the fact that the basic R behaviour is similar to the Emacs C++ style as I happen to look at code in both modes a lot.
I think the only additions I regularly make are to follow the last commented-out snippet:
You can of course turn off the underscore toggle if you really need to code with underscores.
对于 ESS 的开发版本(将于 2015 年 9 月发布),只需添加到
ess-mode-hook
即可:RStudio 还具有默认设置的“垂直对齐参数”复选框。如果您想在未选中此设置时重现该行为(如 Hadley 的代码中所示),则需要将
ess-offset-arguments
更改为prev-line
:请如果您发现 ESS 和RStudio 缩进。
With the development version of ESS (to be released in September 2015), just add to your
ess-mode-hook
:RStudio also has a 'Vertically align arguments' checkbox that is set by default. If you want to reproduce the behaviour when this setting is unchecked (as in Hadley's code), you'll need to change
ess-offset-arguments
toprev-line
:Please file an issue on https://github.com/emacs-ess/ESS if you find an important discrepancy between ESS and RStudio indentation.
Hadley 指南的优点是在运算符周围留出空间(除了 / 周围)
有一个 smart -operator 包几乎为每个操作员实现了它。
这是我的设置(取消注释您要使用的运算符):
另请参阅有关 R 样式的精彩讨论 此处。
[编辑] 我还对全局变量使用 R 代码事实上的驼峰命名风格。局部变量的名称用下划线分隔 - 很容易区分。
emacs 中有一个特殊的
subword
模式,它重新定义了用于大写子词的所有编辑和导航命令The good point of Hadley's guide is spaceing around operators (except maybe around /)
There is a smart-operator package which implements it for almost every operator.
This is my setup (uncoment operators which you want to use):
See also a nice discussion on R styles here.
[edit] I am also using the defacto camelCase style of R code for globals. The underscore-separated names for local variables - it's easy to differentiate.
There is a special
subword
mode in emacs which redefines all editing and navigation commands to be used on capitalized sub-words与 OP 具有相同的风格偏好,我跳到这里,感谢 @lionel 的有效建议,但我在
~/emacs.d/init.el
中设置 RStudio 风格时遇到了问题:Emacs/ESS 拒绝应用样式(4 空格缩进而不是 2 空格,这里不讨论样式:))。
我对新手的建议是,在
~/emacs.d/init.el
中使用以下钩子即可解决问题。
Having the same style preference of the OP, I jumped here and I thank @lionel for your valid suggestions, but I had an issue coming from setting RStudio style in
~/emacs.d/init.el
with:Emacs/ESS refused to apply the style (4-space indent instead of the 2-space, not discussing style here :) ).
My advice for the novice, using the following hook in your
~/emacs.d/init.el
:will do the trick.
风格指南的另一个组成部分到目前为止还没有涉及到
是函数跨多行运行时的缩进。风格指南
说要使用缩进,
但使用任何默认样式似乎都会将行
X
缩进一些固定的值空格数而不是最多
(
。解决方案是 设置ess-arg-function-offset
到一些非数字,例如,这应该放置在类似于@Dirk Eddelbuettel 的答案的模式挂钩中。此外,它必须在任何对 ess-set-style 的调用之后进行,否则它将被覆盖。
One more component of the style guide which hasn't been covered here so far
is indentation when a function runs over multiple lines. The style guide
says to use indentation like
but using any of the default styles seems to indent line
X
by some fixednumber of spaces rather than up to the
(
. The solution is to setess-arg-function-offset
to some non-number, e.g.This should be placed in a mode hook similar to @Dirk Eddelbuettel's answer. Also it must go after any calls to
ess-set-style
or it will be overridden.