我想引入某种子标题/节标题到文件中(聚合,群集分析,模拟...),并在下面编写代码,这样我就可以快速跳转到我想要处理的部分。
我猜我可以只使用注释并搜索它们,但是那样我无法获得概述或索引。 我还考虑使用Org-Mode作为标题,但这对于与另一个编辑器合作的协作者来说可能不太方便。
我知道R-Studio实现了这个功能,所以Emacs也会有解决方法,对吧?
非常感谢!
PS:像imenu这样的工具可以用于函数,但不适用于节标题。
我想引入某种子标题/节标题到文件中(聚合,群集分析,模拟...),并在下面编写代码,这样我就可以快速跳转到我想要处理的部分。
我猜我可以只使用注释并搜索它们,但是那样我无法获得概述或索引。 我还考虑使用Org-Mode作为标题,但这对于与另一个编辑器合作的协作者来说可能不太方便。
我知道R-Studio实现了这个功能,所以Emacs也会有解决方法,对吧?
非常感谢!
PS:像imenu这样的工具可以用于函数,但不适用于节标题。
一般情况下,我使用org-mode和org-babel进行编程,但是当我需要与他人共享脚本时,我会在我的.emacs
中添加以下内容:
(defgroup ess-jb-faces nil
"Faces used by cutomized ess-mode"
:group 'faces)
(defface ess-jb-comment-face
'((t (:background "cornsilk"
:foreground "DimGrey"
:inherit font-lock-comment-face)))
"Face used to highlight comments."
:group 'ess-jb-faces)
(defface ess-jb-comment-bold-face
'((t (:weight bold
:inherit ess-jb-comment-face)))
"Face used to highlight bold in comments."
:group 'ess-jb-faces)
(defface ess-jb-h1-face
'((t (:height 1.6
:weight bold
:foreground "MediumBlue"
:inherit ess-jb-comment-face)))
"Face used to highlight h1 headers."
:group 'ess-jb-faces)
(defface ess-jb-h2-face
'((t (:height 1.2
:weight bold
:foreground "DarkViolet"
:inherit ess-jb-comment-face)))
"Face used to highlight h2 headers."
:group 'ess-jb-faces)
(defface ess-jb-h3-face
'((t (:height 1.0
:weight bold
:foreground "DarkViolet"
:inherit ess-jb-comment-face)))
"Face used to highlight h3 headers."
:group 'ess-jb-faces)
(defface ess-jb-hide-face
'((t (:foreground "white"
:background "white"
:inherit ess-jb-comment-face)))
"Face used to hide characters."
:group 'ess-jb-faces)
(font-lock-add-keywords 'ess-mode
'(("^###\\(#\\)\\([^#].*\\)$" (1 'ess-jb-hide-face t)(2 'ess-jb-h1-face t))
("^###\\(##\\)\\([^#].*\\)$" (1 'ess-jb-hide-face t)(2 'ess-jb-h2-face t))
("^###\\(###\\)\\([^#].*\\)$" (1 'ess-jb-hide-face t)(2 'ess-jb-h3-face t))
("^###\\( .*\\|$\\)" 1 'ess-jb-comment-face t)
("^###" "\\*.*?\\*" nil nil (0 'ess-jb-comment-bold-face append))
))
####
开头的行都会被格式化为“标题1”。任何以#####
开头的行都会被格式化为“标题2”,以此类推。以###
开头的行将被视为具有特殊字体锁定的注释(用于长注释)。这可以产生以下效果:
这种方法有些不太正规,但它的优点在于只使用标准R注释,因此可以与他人轻松共享。通常我将以下内容用作“标题1”:其他人看到的是下面的样子,而我则享受自定义的字体锁定:############################################
#### HEADER 1
############################################
outline-minor-mode
并能够折叠/展开之前定义的部分:(add-hook 'ess-mode-hook
'(lambda ()
(auto-complete-mode nil)
(outline-minor-mode 1)
(setq outline-regexp "\\(^#\\{4,6\\}\\)\\|\\(^[a-zA-Z0-9_\.]+ ?<- ?function(.*{\\)")
(setq outline-heading-alist
'(("####" . 1) ("#####" . 2) ("######" . 3)
("^[a-zA-Z0-9_\.]+ ?<- ?function(.*{" .4)))))
所有这些代码都没有经过很好的测试,而且我远非emacs lisp专家,所以可能有更好的方法来实现它,如果出现错误,请不要感到惊讶!
最近(2013年2月)对Orgmode的更新意味着您现在应该能够在源代码中嵌入org标题,并使用orgstruct-mode浏览它们。 因此,通过git升级您的org模式,然后尝试打开以下示例R文件。 当您位于包含嵌入式org标题的注释行上时,只需按TAB或shift-TAB,您就可以获得org模式标题。
### * Create data
data = list( s1=list(x=1:3, y=3:1),
s2=list(x=1:5, y=1:5), s3=list(x=1:4, y=rep(3,4)))
### * Base graphics version
par(mfrow=c(2,2))
lapply(data, plot)
### * Lattice version
nplots <- length(data)
pts.per.plot <- sapply(data, function(l) length(l$x))
df <- data.frame(which=rep(1:nplots, times=pts.per.plot),
x=unlist(sapply(data, function(l) l$x)),
y=unlist(sapply(data, function(l) l$y)))
xyplot(y~x|which, data=df, layout=c(2,2))
### ** Make the pdf
pdf(file='o.pdf')
xyplot(y~x|which, data=df, layout=c(2,1))
dev.off()
### * End of file
### Local Variables:
### eval: (orgstruct-mode 1)
### orgstruct-heading-prefix-regexp: "### "
### End:
与上述 orgstruct-mode 类似,您还可以使用新的 outshine package,它与 outline-minor-mode 一起使用,将带星号的注释解释为类似 org-mode 的标题。它们可以使用简单的快捷键(如 TAB 等)折叠/展开/添加等。
另一个选择是allout-mode,这是我多年来一直很满意的选择。使用每个文件的本地变量:
## Local variables:
## allout-layout: (-1 : 0)
## End:
"^### \\(\\*\\)\\([^*].*\\)$"
- juba