In general I use org-mode and org-babel, but when I've got to share scripts with others, I've got the following in my .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))
))
With this, any comment with ####
at the beginning of the line is formatted as an "header 1". Any comment with #####
is formatted as an "header 2", etc. And lines which begin with ###
is seen as a comment with a special font-lock (used for long comments).
This can give something like this :
This is quite hacky, but the advantage is that it only uses standard R comments and as such can be shared without problem with others. In general I use the following for "header 1" : others see it as below, while I enjoy my defined font-lock :
############################################
#### HEADER 1
############################################
With this syntax, you can also use the following to activate outline-minor-mode
on the sections defined previously and be able to fold/unfold them :
(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)))))
All this code has not been very well tested, and I'm far from an expert in emacs lisp, so there should be better ways to do it, and don't be surprised in case of bugs !